Two views, One ViewModel, the RegionManager using the PRISM MEF Extensions Library

Topics: Prism v4 - Silverlight 4, Prism v4 - WPF 4
Aug 31, 2010 at 1:00 PM

Hi guys

I have two views that is registered to two different parts of the shell via the region manager. These two views need to share the same viewmodel. How can I go about achieving this, without newing up to different instance of the same viewmodel?

Thanks in advance.

Aug 31, 2010 at 3:12 PM

Hi,

Based on our understanding you need to assign the same instance of a particular ViewModel to different views. It is possible to achieve by using the Lifetime Managers of Unity. For more information about this topic you could take a look at the following documentation section on MSDN:

In other words, it consists on resolving a registered instance in your container, which has the singleton behavior. Some time ago, other user has asked a pretty similar question to this, so you could check this:

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Aug 31, 2010 at 3:31 PM
Edited Aug 31, 2010 at 3:32 PM

Hi 

I am amware of the "RegisterType<T>" method in the IUnityContainer. Is there an equivalent export part in the MEF Extension package of the Prism 4.0 CTP - August 2010 that will allow me to implement that same type of functionality?

Thanks.

Aug 31, 2010 at 4:32 PM
Edited Aug 31, 2010 at 4:44 PM

Hi,

In MEF it is possible to export a part with singleton behavior by using creation policies. In this case, it might help to achieve this scenario the usage of the following creation policy, as it is shown below: 

[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(IYourViewModel))]
publicYourViewModel()
{
    ...
}
...
[Import()]
publicYourViewModel sharedViewModel { get; set; }

If your viewmodel should not be imported unless it will be shared, you could guarantee this using the following argument in your import:

[Import(RequiredCreationPolicy = CreationPolicy.Shared)]

For more information about creation policies, you could take a look at the following links:

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Aug 31, 2010 at 4:46 PM
Edited Sep 1, 2010 at 5:47 AM

Please see post below: 

Sep 1, 2010 at 5:11 AM
Edited Sep 4, 2010 at 3:48 AM

Hi, it still didnt work

I tried that, but it sill creates multiple instances (no singleton) of the view model. Here is a break down of what Im doing to better explain my conundrum:

 

      //Initialize the module when available. Register TWO or more views for each module
      public void Initialize()
      {
         this.regionManager.RegisterViewWithRegion("RegionMain", typeof(View.CategoryView));
         this.regionManager.RegisterViewWithRegion("RegionEditor", typeof(View.CategoryEditor));
      }

 

Then in view, I bind the DataContext to the the viewmodel via the MEF Import / Export Attribute

// **** User Control View *******/
[Import(RequiredCreationPolicy = CreationPolicy.Shared)]
public ICategoryViewModel CategoryViewModel { get; set; }


//**** ViewModel**********/
 [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
   [Export(typeof(ICategoryViewModel))]
   public class CategoryViewModel : BaseViewModel, ICategoryViewModel {

In the constructor of the ViewModel, I do a async ria service call to fetch the categories. This should surely only do one call for both views because of the CreationPolicy.Shared (Singleton) attribute , but I actually get three calls made, not even two! Here is the call stack

Call 1: Microsoft.Practices.Composite.MefExtensions!Microsoft.Practices.Composite.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(System.Type serviceType = {System.RuntimeType}, string key = null)
Call 2: Category!Category.View.CategoryView.CategoryView() Line 35	C#
Call 3: Category!Category.View.CategoryEditor.CategoryEditor() Line 28	C#

So I guess, it once for when the module is initialized, once for the CategoryView and once for the CategoryEditor.

Thank you in advance

Sep 1, 2010 at 2:45 PM

Any ideas?

Sep 3, 2010 at 9:41 PM

Hi,

I created a simple sample around this topic and it worked. You could download it from here.

If you continue experiencing this problem, you could share either any additional information or your sample to clarify this scenario.

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Sep 4, 2010 at 3:52 AM
fantivero wrote:

Hi,

I created a simple sample around this topic and it worked. You could download it from here.

If you continue experiencing this problem, you could share either any additional information or your sample to clarify this scenario.

Please let me know if this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Thank you for your help, but not to worry. I eventually did fix the problem/bug.