Experiments using MEF, MVVM and Silverlight 4 Beta – Part 5: Enabling Blend and the Visual Studio designer

Note – this is a multi part post:

In the last post I’ve updated the sample project introducing PartCreator<T>, in this one I will illustrate a method to make available sample data during the editing of the solution in Blend or in the Visual Studio designer, in order to obtain a result like this (a similar approach is used in the awesome MVVM light toolkit by Laurent Bugnion):

MEFMVVM Blend

First of all, I’ve created a class ViewModelProvider to obtain dinamically an instance of the ViewModel and directly used in xaml to create the VM instance, in this way it’s possibile to see sample data during the design phase:

public class ViewModelProvider : IViewModelProvider
{
    public ViewModelProvider() { }

    [Import(typeof(MainPageViewModel))]
    public IViewModelBase mainPageViewModelProvider { get; set; }

    ///

    /// Get the Instance of the ViewModel
    /// 

    public IViewModelBase GetVMInstance
    {
        get
        {
            //Verify if Design Mode
            if ((Application.Current == null) || (Application.Current.GetType() == typeof(Application)))
            {
                return new MainPageViewModel();
            }
            else
            {
                //If not in Design Mode uses MEF to compose the objects
                PartInitializer.SatisfyImports(this);
                return mainPageViewModelProvider;
            }
        }
    }
}

I’ve not already found a method to activate MEF compositions during design time, so an instance of the ViewModel class is explicitely created in code if we are in the tool designers, otherwise SatisfyImports() executes the MEF magic as usually.

It’s now necessary to initialize the properties values in order to display sample data at design time, this step can be done in the constructor of the MainPageViewModel:

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(MainPageViewModel))]
public class MainPageViewModel : ViewModelBase, IMainPageViewModel
{
    ///
    /// Default constructor
    ///
    public MainPageViewModel()
    {
        if ((Application.Current == null) || (Application.Current.GetType()==typeof(Application)))
        {
            //Initialize the properties with test data if design mode
            aViewModelProperty = "Value - Design Mode";
            dataItems = new SampleDataItems(); dataItems.ToList().ForEach(d=>d.Description+=" - Design Mode");
        }
    }

    ///
    /// A sample property
    ///
    [Import("aViewModelPropertyTextProvider")]
    public string aViewModelProperty { get; set; }

    ///
    /// A sample collection
    ///
    [Import(typeof(DataItems))]
    public DataItems dataItems { get; set; }

    ///
    /// A Part creator for the addDataItemCommandCreator
    ///
    [Import(typeof(ICommand))]
    public PartCreator addDataItemCommandCreator { get; set; }

    private ICommand _addDataItemCommand;
    public ICommand addDataItemCommand
    {
        get {
            if (_addDataItemCommand==null)
                _addDataItemCommand = addDataItemCommandCreator.CreatePart().ExportedValue;
            return _addDataItemCommand;
        }
    }
}

Inside Visual Studio, it’s now possibile to edit the User Interface and visualize the sample data defined in the constructor:

VisualStudio2010

The source code is available for download here.

Hope this helps!

8 thoughts on “Experiments using MEF, MVVM and Silverlight 4 Beta – Part 5: Enabling Blend and the Visual Studio designer”

  1. Hi Davide,
    Compared to MVVM Light, isn’t this approach too cumbersome? what’s the plus of using it?

  2. Hi Corrado,
    well it’s a different approach using a different framework of course, I think there will be advantages in terms of extensibility, decoupling and when using the framework to let return results of async calls (a sort of events/messages but framework-driven, I’m investigating :)).
    Thanks!

  3. @Corrado:
    Oh, I also really like the idea to use a a MVVM approach using only libraries available in the standard framework without depending from external ones like Prism.
    Ciao!

  4. Why not use the blend to create sample data based on the viewmodel. dont need to keep updating the design data, when the viewmodel changes(properties added/removed)

    if we allow the Viewmodel property to be null, we dont even need the locator

    any thoughts?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *