Let SilverLight MVVM use Expression Blend when designing data?

I am a strong supporter of Silverlight’s MVVM mode. Currently, I connect the ViewModel to the View by viewing the ViewModel in the code behind the view, so:

< p>

public partial class SomePage: UserControl
{
public SomePage()
{
InitializeComponent();

/ / New up a ViewModel and bind to layout root
var vm = new SomeViewModel();
LayoutRoot.DataContext = vm;
}
}

Then All bindings are handled in View, all logic is handled in ViewModel as shown in the pattern.

However, connecting them in this way means that the designer does not work well and I cannot use Expression Blend design time data. I know that there are some libraries like MVVM Light that can help achieve all of this, but I don’t want to introduce a library because it is “there is one more thing” to deal with.

In maintenance While the designer functions, is there a simple mode for MVVM to connect to Silverlight, especially in Blend? I have done some Google searches, but with so many outdated articles and so much confusion between WPF and Silverlight and the old version, it is difficult for me to figure out which to use.

By the way, if If it is important, I will focus on using VS2010 SL4.

You can use several methods. < p>

First, let the Sample Data and design-time properties of Expression (ie d: DataContext) take over the designer. In your code, you only need to adjust the view model binding:

< p>

if (!DesignerProperties.IsInDesignTool)
{
var vm = new SomeViewModel();
LayoutRoot.DataContext = vm;
}

Secondly, you can use the binding special design-time view model:

LayoutRoot.DataContext = DesignerProperties.IsInDesignTool ?
new DesignViewModel(): new MyViewModel();

Finally, another way is to manage the data in the view model. I don’t like this because it spreads the responsibilities among all view models, but you have higher precision:

// constructor
private Widget[] _designData = new[] {new Widget("Test One"), new Widget("Test Two") };

public MyViewModel()
{
if (DesignerProperties.IsInDesignTool)
{
MyCollection = new ObservableCollection(_designData);
}
else
{
MyS ervice.Completed += MyServiceCompleted;
MyService.RequestWidgets();
}
}

private void MyServiceCompleted(object sender, AsynchronousEventArgs ae)
{
// load up the collection here
}

Hope it helps!

I am a strong supporter of Silverlight's MVVM mode. Currently, I connect the ViewModel to the View by viewing the ViewModel in the code behind the view, so:

< /p>

public partial class SomePage: UserControl
{
public SomePage()
{
InitializeComponent();
< br /> // New up a ViewModel and bind to layout root
var vm = new SomeViewModel();
LayoutRoot.DataContext = vm;
}
}

Then all the bindings are handled in the View, and all the logic is handled in the ViewModel as shown in the pattern.

However, connecting them in this way means that the designer does not work well , I can’t use Expression Blend to design time data. I know that there are some libraries like MVVM Light that can help achieve all of this, but I don’t want to introduce a library because it is "there is one more thing" to deal with.

< p>While maintaining the designer function, is there a simple mode for MVVM to connect to Silverlight, especially in Blend? I have done some Google searches, but with so many outdated articles and so much confusion between WPF and Silverlight and the old version, it is difficult for me to figure out which to use.

By the way, if If it is important, I will focus on using VS2010 SL4.

You can use several methods.

First, let Expression's Sample Data and design-time properties (ie d: DataContext) take over the designer. In your code, you only need to adjust the view model binding:

if (! DesignerProperties.IsInDesignTool)
{
var vm = new SomeViewModel();
LayoutRoot.DataContext = vm;
}

Secondly, you can use binding Special design-time view model:

LayoutRoot.DataContext = DesignerProperties.IsInDesignTool ?
new DesignViewModel(): new MyViewModel();

Finally, another way is to manage the data in the view model. I don’t like this because it spreads the responsibility among all the view models, but you have higher precision:

< pre>// constructor
private Widget[] _designData = new[] {new Widget("Test One"), new Widget("Test Two") };

public MyViewModel()
{
if (DesignerProperties.IsInDesignTool)
{
MyCollection = new ObservableCollection(_designData);
}
else
{
MyService.Completed += MyServiceCompleted;
MyService .RequestWidgets();
}
}

private void MyServiceCompleted(object sender, AsynchronousEventArgs ae)
{
// load up the collection here< br />}

I hope it helps!

Leave a Comment

Your email address will not be published.