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

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

问题描述

我在WPF中将PRISM 4 Navigation API与Uni​​ty一起使用.我有一个树视图,它启动一个 RequestNavigate ,并传入所选树节点的ID( GUID ).

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

在我的模块中,我已经这样注册了view/view-model:

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

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

当我从树形视图中选择不同的节点时, DetailsRegion 会按预期显示 SiteDetailsView ,但是当我想导航回同一节点时,会出现一个新视图/view-model已创建.

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.

我试图在IsNavigationTarget(NavigationContext navigationContext)中断,但似乎从未调用过此方法.

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

我哪里出问题了?预先感谢.

Where have i gone wrong? Thanks in advance.

推荐答案

问题出在我从未想到的地方...调试导航API导致我进入RegionNavigationContentLoader

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"

一切正常.

结论

ViewModel的名称必须以 和你以前的名字一样 确定您的观点.

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

这似乎也起作用.因此,似乎View-Model的名称与用于导航到该视图的视图名称具有内在联系.

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.

注意

仅当您将IoC和Unity与PRISM 4 Navigation API一起使用时.使用MEF时似乎不会发生这种情况.

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.

进一步调查

我也知道某些

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天全站免登陆