Same View Created Multiple Times

Topics: Prism v4 - Silverlight 4
Apr 20, 2011 at 10:48 AM


In ModuleInit I am loading the same view into the region twice by using different parameters in the  constructor, like:

 

this.regionManager.Regions["MainNavigationRegion"].Add(new FillRateNavigationItemView("Overall"), "Overall");

this.regionManager.Regions["MainNavigationRegion"].Add(new FillRateNavigationItemView("Projectors"), "Projectors");

 

I have ViewModel attached to the view like:

[Import]

public EpsonColumnViewModel ViewModel {

    set { this.DataContext = value; }

    get { return (EpsonColumnViewModel)this.DataContext; }

}

 

The problem is that ViewModel is instantiated only once and both view display the same values. I would need the ViewModel to calculate values for both instances of the view. What is a good approach here?

Apr 20, 2011 at 3:50 PM

Hi,

Based on my understanding of your scenario, you need to decorate your EpsonColumnViewModel class with the PartCreationPolicy attribute as NonShared.

By doing this, a new instance of the exported view model will be created each time it is resolved. For example in your code it would look like this:

[Export(typeof(IEpsonColumnViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class EpsonColumnViewModel : IEpsonColumnViewModel
{
   …
}

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

Apr 21, 2011 at 6:38 PM

Yeah. That's it. Thanks Miguel.