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

我的未来我决定 提交于 2019-12-06 03:03:55

问题


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.


回答1:


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; }
    }
}



回答2:


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.




回答3:


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);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/5115484/how-to-create-a-new-view-every-time-navigation-occurs-in-prism

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!