PRISM WPF - 导航每次都会创建新视图 [英] PRISM WPF - Navigation creates new view every time

查看:34
本文介绍了PRISM WPF - 导航每次都会创建新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm using PRISM 4 Navigation API with Unity in WPF. I have a tree-view that initiates a RequestNavigate passing in the selected tree node's ID (GUID).

_regionManager.RequestNavigate(RegionNames.DetailRegion,
    ViewNames.SiteView + "?ID=" + site.ID);

In my module, I have registered the view/view-model like so:

_container.RegisterType<SiteDetailsViewModel>();
_container.RegisterType<object, SiteDetailsView>(ViewNames.SiteView);

When I select different nodes from the tree view, the DetailsRegion displays the SiteDetailsView as expected, but when I like to navigate back to the same node, a new view/view-model is created.

I tried to break at IsNavigationTarget(NavigationContext navigationContext) but this method appears to never be called.

Where have i gone wrong? Thanks in advance.

解决方案

The problem was in such a place that I never expected... Debugging the Navigation API lead me to the RegionNavigationContentLoader

public object LoadContent(IRegion region, NavigationContext navigationContext)

When i stepped further down the code, I noticed a call to:

protected virtual IEnumerable<object> GetCandidatesFromRegion(
    IRegion region,
    string candidateNavigationContract)

I noticed that the naming here is key to matching the view to the view-model.

In my example, the name for each part was:

public class SiteDetailsViewModel { ... } // ViewModel

public class SiteDetailsView { ... } // View

ViewNames.SiteView = "SiteView" // ViewName constant

When I inadvertently made the following change:

ViewName.SiteView = "SiteDetailsView"

Everthing worked.

Conclusion

The name of the ViewModel must start with the same name you used to identify your view.

I tested this out by changing my view to:

public class MyView { ... }

and still using the same view name to register with the container and navigation:

_container.RegisterType<object, MyView>(ViewNames.SiteView);

...

_regionManager.RequestNavigate(RegionNames.DetailRegion,
    ViewNames.SiteView + "?ID=" + site.ID);

This seems to work also. So it seems the name of the View-Model is intrinsically linked to the view name used to navigate to that view.

NOTE

This is only when you're using IoC and Unity with the PRISM 4 Navigation API. This doesn't seem to happen when using MEF.

Further Investigation

I am also aware that some guides have told us to use the typeof(MyView).FullName when registering the view with the Container...

_container.RegisterType<object, MyView>(typeof(MyView).FullName);

I personally think this is a mistake. By using the view's full name, you are creating a depending between the view and any one who wishes to navigate to that view...

_regionManager.RequestNavigate(RegionNames.DetailRegion,
    typeof(MyView).FullName + "?ID=" + site.ID);

这篇关于PRISM WPF - 导航每次都会创建新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆