简体   繁体   English

视图模型中的WPF页面导航

[英]wpf page navigation in view model

in my view I have a button. 在我看来,我有一个按钮。 The button has a Relay Command, which do some action. 该按钮具有中继命令,该命令可以执行某些操作。 After this action it should navigate to an other site. 执行此操作后,应导航到其他站点。 How can I do this. 我怎样才能做到这一点。 How can I navigate to this site inside my viewModel? 如何在我的viewModel中导航到该站点? I hope you understand what I mean. 我希望你明白我的意思。

Thanks for your help holzleube 感谢您的帮助holzleube

Unfortunately, the answer to that question may not be as straightforward as you'd like. 不幸的是,该问题的答案可能并不像您想要的那么简单。 I think everybody has a slightly different way of doing it, so this is just one way. 我认为每个人的执行方式都略有不同,所以这只是一种方式。 First off, we use the Prism/CAL library from Microsoft, which has this notion of "regions", which are named placeholder controls (ie ItemsControl, sort of). 首先,我们使用Microsoft的Prism / CAL库,该库具有“区域”的概念,这些概念称为占位符控件(即ItemsControl等)。 Then on all of our "pages", we take in an instance of a controller we make called a NavigationController, whose duty it is to swap out views. 然后,在我们所有的“页面”上,我们采用一个称为NavigationController的控制器实例,该控制器的职责是交换视图。 The NavigationController is full of methods like "GoToHome" which do things like: grab the Home ViewModel from dependency injection (or make a new instance, depending on your setup), clear out a certain region by obtaining it from the region manager, then load it with the new Home view model. NavigationController充满了诸如“ GoToHome”之类的方法,这些方法的作用如下:从依赖项注入中获取Home ViewModel(或根据您的设置创建一个新实例),通过从区域管理器中获取某个区域来清除该区域,然后加载搭配新的主视图模型。 Then we wire up the call in our "page"'s backing view model to call the controller. 然后,将呼叫连接到“页面”的后视图模型中以调用控制器。

So, in summary, it takes some plumbing. 因此,总而言之,这需要花费一些时间。 Once you've got the initial setup, it only takes a few minutes to add a new page, but there is no magical "link" because you have to know where you're going, where it loads, and how to obtain the backing viewmodel. 完成初始设置后,只需几分钟即可添加一个新页面,但这里没有神奇的“链接”,因为您必须知道要去的地方,要加载的地方以及如何获得支持视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM