Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new view every time navigation occurs in PRISM?

Tags:

c#

wpf

prism-4

I'm using WPF4 and PRISM4 for my new project.

There is a single module with several views in it. The DI is done with unity. When I navigate from ViewA to ViewB for the first time, the ViewB is created and its constructor is called. But when I try to navigate to ViewB for the second, third time, the ViewB is not created, but existing instance is reused.

I'm using IRegionManager.RequestNavigate for my navigation purposes.

I've tried to pass TransientLifeTimeManager to RegisterType Unity methods, but to no avail.

Is there a way to configure prism and/or unity to create a new view every time I navigate to it?

Thanks.

like image 258
Valentin V Avatar asked Feb 25 '11 09:02

Valentin V


3 Answers

The correct way to do this is by implementing INavigationAware either in your View or your ViewModel (Prism will check first the view, and if it doesn't implement INavigationAware it will also check the ViewModel).

You are interested specifically in the IsNavigationTarget method, which tells Prism if the current instance of the View should be reused, or if another instance should be created to satisfy the navigation request. So, to always create a new View you would do:

public class MyViewModel : INavigationAware {
    bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
    {
        return false;
    }

    void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
    {
    }
}

All of this is explained in greater detail in Chapter 8 of the Prism 4 documentation; they also have an illustration of how it works, which is very nice because it also lets you know exactly where you can hook in and how.

like image 87
Jon Avatar answered Oct 12 '22 06:10

Jon


The way to do it is to implement IRegionMemberLifetime on your either your view or viewModel, and return false in the boolean property KeepAlive as follows:

public class EmployeeDetailsViewModel : IRegionMemberLifetime
{
    public bool KeepAlive
    {
        get { return false; }
    }
}
like image 38
Elad Katz Avatar answered Oct 12 '22 06:10

Elad Katz


It internally looks for a View in ActiveViews property of a region. If it does not exist in there, it creates a new one and adds it to ActiveViews for future use.

To accomplish what you want to do, you will need to remove or clear ActiveView collection before navigating to any View.

Example:

public static class RegionManagerExtensions
{
    public static void RequestNavigateEx(this IRegionManager regionManager, String regionName, Uri source)
    {
        if (regionManager != null)
        {
            IRegion region = regionManager.Regions[regionName];

            if (region != null)
            {
                foreach (Object view in region.ActiveViews)
                {
                    region.Remove(view);
                }

                regionManager.RequestNavigate(regionName, source);
            }
        }
    }
}
like image 38
decyclone Avatar answered Oct 12 '22 06:10

decyclone