Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design time data in WPF

[using vs2010 & expression blend v4]

Hi - trying to load up some design time data in WPF and Blend, using Josh Smith's concept here: http://joshsmithonwpf.wordpress.com/2010/04/07/assembly-level-initialization-at-design-time/ e.g.

[AttributeUsage(AttributeTargets.Assembly)]
public class DesignTimeBootstrapperAttribute : Attribute
{
    public DesignTimeBootstrapperAttribute(Type type)
    {
        var dep = new DependencyObject();
        Debug.WriteLine("here..?");
        if (DesignerProperties.GetIsInDesignMode(dep))
        {
            // TODO: Design-time initialization…
            IBootstrapper instance = Activator.CreateInstance(type) as IBootstrapper;
            if (instance != null)
            {
                instance.Run();
            }
        }
    }
}

With my attribute here in AssemblyInfo.cs, where AppBootstrapper extends MefBootstrapper.

[assembly: AssemblyCopyright("Copyright ©  2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: DesignTimeBootstrapper(typeof(AppBootstrapper))]

I don't want to use the Blend sample data, a) as it doesn't seem to create data for ObservableCollection and b) I'm in design mode by definition, so things will change quite a lot, but my 'generated data' will not.

Anyway, nothing seems to be happening.

Q1: How is it possible to debug the design time initialisation of my bootstrapper? Q2: Do I need additional blend namespaces/ attributes etc in my View XAML?

(In my bootstrapper I'm just registering a different module where I want to replace RunTimeService with a DesignTimeService, exporting the IService interface).

TIA

like image 880
Ian Avatar asked Jan 17 '11 18:01

Ian


1 Answers

To debug this:

  • Open your project in VS2010
  • Set a breakpoint in the assembly attribute constructor
  • Start a new instance of Blend 4
  • From VS2010 use Debug -> Attach to Process: and choose Blend
  • Switch to Blend and open your project
  • Open a XAML file that references your sample data

Also, any Debug.WriteLine should appear in the VS2010 output window.

If you can't get the attribute method to work (I haven't tried it myself), you can use this method (which I have used) from MVVM Light:

private bool? _isInDesignMode;

public bool IsInDesignMode
{
    get
    {
        if (!_isInDesignMode.HasValue)
        {
            var prop = DesignerProperties.IsInDesignModeProperty;
            _isInDesignMode =
                (bool)DependencyPropertyDescriptor
                .FromProperty(prop, typeof(FrameworkElement))
                .Metadata.DefaultValue;
        }

        return _isInDesignMode.Value;
    }
}
like image 83
Rick Sladkey Avatar answered Nov 15 '22 08:11

Rick Sladkey