Creating multiple instances of PRISM views with MEF
In my application I want to create multiple instances of the same view using PRISM/MEF
Both my view and the view model has got PartCreationPolicy non shared but it doesn't create multiple views. Instead it changes the content in the already opened view.
Top part of the Customer view
[ViewExport("CustomerView")]
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class CustomerView : UserControl
{
开发者_Python百科 public CustomerView()
{
InitializeComponent();
}
Top part of the CustomerViewModel
//[Export(typeof(CustomerViewModel))]
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class CustomerViewModel : NotificationObject, INavigationAware
{
private ICustomerRepository _custRepository;
This is how I call the view passing customer id.
UriQuery parameters = new UriQuery();
parameters.Add("CustomerID", CurrentItem.CustomerID.ToString());
_regionManager.RequestNavigate(RegionNames.MainRegion,
new Uri("CustomerView" + parameters.ToString(), UriKind.Relative),
NavigationCompleted);
When I call the view using ServiceLocator.GetInstance it opens up a new view everytime but I cannot pass the customer ID.
_regionManager.Regions[RegionNames.MainRegion].Add(_serviceLocator.GetInstance<CustomerView>());
Please help me on this. What I exactly want to do is create multiple instances of same view passing some ID (customer ID for example)
Have you implemented IsNavigationTarget on your ViewModel so that it returns false if the customerIDs don't match?
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return navigationContext.Parameters["CustomerID"] == this.CustomerID.ToString();
}
精彩评论