8

[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

Ian
  • 275
  • 6
  • 17

1 Answers1

3

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;
    }
}
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • 1
    So in your ViewModel do you check whether it's true and then return your fake data from a VM property, like an ObservableCollection? The problem I could see then would be wiring up the ViewModel and the View as I'm using MEF and Prism. I'll have a look at the way you said to wire up vs2010 and blend then maybe I can get that working. Thanks. – Ian Jan 21 '11 at 12:00