Silverlight MVP Laurent Bugnion of IdentityMine appears on the show to discuss using MVVM with Silverlight. Laurent and John discuss their experiences with MVVM and how Laurent's experiences inspired him to create his MVVM Light Toolkit. If you have been meaning to get into MVVM or you feel a bit overwhelmed by it all, definitely watch this episode and check out the MVVM Light Toolkit. Follow @SilverlightTV on Twitter for the latest updates.
Thanks John & Laurent; Great show!
One question for Laurent. Let's someone creates a solution using RIA Services template, which adds a few nice items for authorization and etc. Now you have your solution and projects created. Then you decide to use MVVM Light, since you can't start creating a new solution to choose MVVM light templates, how do you get to add all the features MVVM light templates to an already created solution?
Secondly what would be the advantage of using MVVM light over Prism4 and the same question in the opposite direction, what is advantage of using Prism 4 over MVVM light?
Thanks!
Hi Ben, and thanks!
About your first question: Adding the MVVM Light to an application is actually very easy, and I am preparing a blog post showing how to do that. I will publish this ASAP. It is a matter of adding the DLL and then wiring up the objects together in XAML (or in Blend...)
The main advantage of MVVM Light over Prism is that it is lighter and easier to add in an application. Also, it is very strongly aimed to Blendability, and all the components are created with this in mind. I generally recommend the following: If you have an application using Prism already (for its composition features for example), then it might make more sense to use the Prism components for the MVVM application, in order to avoid adding too many dependencies. If you don't, I would rather recommend MVVM Light, because it is lighter and easier.
Hope this helps!
Laurent
Ben - Here is a very quick rundown of Prism compared to MVVM Light, all off my head in 3 minutes
However, keep in mind that you can still handle regions on your own with MVVM Light Toolkit, use MEF or Unity or Ninject with it, and use MEF for moduel catalogs. So either choice is excellent depending on what you need.
This is a short overview ... I'll be sure to post more MVVM details on my blog at johnpapa.net and Silverlight TV since it is one of my favorite topics.
M-V-VM Light support for Blendability is absolutely awesome, think that use of MEF in future versions can simplify it further and easily add support to Regions and Catalog like feature.
Gentlemen, thank you for detail replies;John, I was thinking of the same as far as using MEF for Modules and Regions, since I like it's implementation better than Prism. However, in my original post I purposely post fixed Prism with "4" since many things are going to change compared to 2, including of MEF’s finding modules. I think it would be good to bring one of the PMs from Prism team to SLTV to talk about Prism 4.
Ben - It would be good. I'll see if we can get some Prism 4 love .
Hi Laurent,
Thanks for this great toolkit. Your toolkit is nice but if you add a single how to use documentation to it, it would be awesome. I know you did most of it in blog posts but if you gather them in a single doc it would be great. Thanks.
One question I have is about the Locator Class. Could you please explain more about this class and how it does the work.
It might be a stupid question but Why when I do not use the locator class and just Bind the DataContext of the main container object to the VM such as below the Command do not executed.
<UserControl
xmlns:vm="clr-namespace:MyApp.ViewModels"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras">
<UserControl.Resources>
<vm:MainPageViewModel x:Key="MainPageViewModel"/>
</UserControl.Resources>
<Grid DataContext="{Binding MainPageViewModel}">
<Button Content="Simple Command"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <cmd:EventToCommand Command="{Binding SimpleCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button>
</Grid>
</UserControl>
and in the MainPageViewModel class
namespace MyApp.ViewModels{ public class MainPageViewModel: ViewModelBase {
public MainPageViewModel() {
SimpleCommand= new RelayCommand(() => { // do sth
Status = "SimpleCommand executed"; });
}
public RelayCommand SimpleCommand { get; private set; }
As you see I have set the DataContext of the Grid to my VM so my button should be able to use the SimpleCommand, am I right?
Nore: The above code is just a sample. I know that the button now has built in command support now in SL4.
Hi Ali,
In your sample, you declared the MainViewModel as a resource. Hence, you must bind to the resource, using the StaticResource extension.
<Grid DataContext="{Binding Source={StaticResource MainPageViewModel}}">
This is a fairly simple way to create and bind your data context, but it has a few disadvantages that the Locator solves:
- You cannot pass parameters to the ViewModel constructor.
- You don't have control over the creation (and deletion) of the ViewModel. The parser will create it when the line in the resources is reached (in Silverlight... in WPF the ViewModel is created when it is used by a StaticResource extension). This makes it impossible to create the ViewModel beforehand, for example when the application starts.
I hear you about the documentation. I am busy writing my next book now, so it won't be before a moment, but I have a series of posts titled MVVM Light 101 showing how to get started with each component. Great idea gathering them all in a single page. I am also going to extract the XML documentation from the code to document the API in an easier way.
Thanks
Hey John,
Not sure what you mean with WF/SL code sharing. The MVVM Light Toolkit uses exactly the same syntax (and about 80% of the same code ) in WPF and in Silverlight. This is one of the strength of the framework actually. I am very careful to keep the code compatible.
Cheers,