Question

I am trying to control when a new view is created and when an existing view is shown.

This is a very similar scenario as outlined in the "Navigating to Existing Views" section in the Prism documentation, but I can't get it to work fully: http://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx

I am finding I can create the view/view model to begin with ok, but I am then unable to create a new instance of it. I.e. I want more than one instance to exist at once.

Here's an example of the view model:

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class DataEntryPageViewModel : INavigationAware, IRegionMemberLifetime
{
    private Guid id;

    [ImportingConstructor]
    public DataEntryPageViewModel()
    {
        id = Guid.NewGuid();
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        // In actual fact there would be more logic here to determine
        // whether this should be shown to the user
        return false;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
    }

    public bool KeepAlive
    {
        // For the purposes of this example we don't want the view or the viewModel
        // to be disposed of.
        get { return true; }
    }
}

I am navigating to this as follows:

m_RegionManager.RequestNavigate(
               "MainRegion",
               new Uri("/DataEntryPageView", UriKind.Relative));

So the first time I call the above the view is shown.

The next time I call RequestNavigate the IsNavigationTarget is hit and it returns false. What I then want it to do is to create a new instance but that doesn't happen. I know it's not happening because the constructor does not get hit and the UI does not update to show the new instance of the view.

Any ideas how I can make it create a new instance?

Many thanks,

Paul

Edit

I have noticed that the second time I call RequestNavigate (to request another instance of the same view) the callback reports an error "View already exists in region." It therefore seems that I can have multiple instances of different views in a region, but not multiple instances of the same view. My understand of this isn't great though so I could be wrong.

Was it helpful?

Solution 3

I have now been able to get this to work, it was because I didn't have

[PartCreationPolicy(CreationPolicy.NonShared)]

on the class declaration of the view. I had it on the ViewModel.

So this is now resulting in the behaviour I expected.

Thanks though to Zabavsky and Alan for your suggestions.

OTHER TIPS

Why are you not creating the view when you want a new one to be created? It looks to me like you are using MEF.

  1. Use the container to resolve a new instance of your view
  2. Add the new instance of the view to the MainRegion
  3. Then call Navigate and handle the appropriate logic in IsNavigationTarget

You should use the [Export] attribute in your view with a contract name: [Export("DataEntryPageView")].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top