‘Silverlight unleashed’: How to use ViewModelLocator with multiple MVVM assemblies?

Jan 25, 2011 at 9:47 AM

Hi Laurent,

I'm just reading your book "Silverlight unleashed".
In Chapter 20 your are explaining how to use MEF to load plugins and how to use your messenger class. But you are not explaining how to load plugins, which where build with the MVVM light toolkit.

It would be interesting to know how to use MVVM in your BingTextSearch and BingImageSearch plugins and how to handle the plugins via MEF and a global (or local) ViewModelLocator.
If I put View and Viewmodel in separated plugins/assemblies where do I instantiate the ViewModelLocator. Usually I put to it in the App.xaml file of the StartUp project.
    <Application.Resources>
        <Mvvm:ViewModelLocator x:Key="Locator" />
    </Application.Resources>
But I also need a reference in the view
DataContext
="{Binding Source={DynamicResource Locator}, Path=PersonVm}"
So I need also a reference in the plugin to the ViewModelLocator. And the ViewModelLocator need a reference to the different viewmodels.
But then I have no decupled plugins.

I like your book, but you ignore MVVM in the chapter 20 (Building Extensible and Maintanable Applications) completely and put all code in the codebehind file.
Ok, I don’t want to discuss if it’s ok to use the codebehind file, but I think at this point you were a little bit to lazy. ;-)

But maybe it is not a good idea to use MEF and MVVM light to seperate MVVM parts in different assemblies.
Should I use bigger frameworks like Prism?

Regards
Dirk

Jun 30, 2011 at 1:25 PM

Hello Dirk

 As there was no reply here and I am facing exactly the same question I am beginning to wonder if I should be using a different framework too.  ;-)

So, a quick last stab, as I actually would prefer to stay with MVVMLight, is it possible to use the toolkit in multiple assemblies, i.e. the View in one assembly and the ViewModel in the main application.

If so, can someone point me in the right direction?

Thanks

Coordinator
Jun 30, 2011 at 1:50 PM

Hi,

Sorry about missing this message. 

ViewModelLocator (VML) is a good way to expose viewmodels when these are static. For dyanmically loaded objects (such as those loaded through MEF), the VML is more difficult to use. There are a few possibilities:

  • Bind the dynamically loaded VMs to the DataContext of the view in code. The blendability can then be reached with the d:DataContext design time construct, as explained in http://www.galasoft.ch/mvvmvideo2
  • If the loaded component is in its own XAP file, use a design time VML in this XAP file (placed in App.xaml of this particular XAP file). This will be available at design time in Blend, but not at run time when the XAP is loaded through MEF into the application. In this case, assign the VM dynamically to the DataContext (like before).
  • Create the secondary VML and save it in the application's resources dynamically when the component is loaded through MEF.

Hopefully this helps,

Laurent

Jun 30, 2011 at 7:22 PM

Hello Laurent,

Thanks for replying.

I watched the video and learned alot, thanks for this presentation, very informative.

However, from your comment above, you talk about a dynamcially loaded ViewModel.  In my case I want to Dynamically load the view, and somehow attach the ViewModel which is in the main application code.

Can you give me an example of how to do this if it is possible.  I am trying to 'find' the ViewModel that is in the application space using the Locator but can't see how to do this.

Thank you for your patience with me, I am pretty new to the MVVM pattern  :-)

Regards

Mark.

Apr 20, 2012 at 4:16 PM

Hi Mark & Laurent,

I am trying to accomplish the same thing, Mark. Basically I am trying to create a SLOOB that rotates a number of charts in a loop. Each chart will be it's own View and have it's own ViewModel to acquire it's data. I'm thinking that I'll use the Telerik transisition control on the MainPage.xaml and use the ViewModelLocator to discover and dyanmically load each view. But I am not at all sure how to do that.

Mark, have you been able to figure this out?

 

-Sam