如何使用 MVVM Light for WPF 在窗口中导航?
我刚刚开始一个新项目,其中表示层将由 WPF 完成,MVVM Light 由 GalaSoft 完成。
我需要大量视图,但我不清楚如何通过窗口管理导航。
首先,MVVM Light 中提供的用于创建新的“WPF MVVM 视图”的模板创建了一个新的 Window
,该新的 Window
无法用于按帧导航(我的意思是,通过将帧放入 < code>mainView 并更改要导航的源路径)。
对于使用模板创建的所有视图,我是否只需将 Window
更改为 Page
即可?
或者是否有其他方法可以使用 MVVM Light 工具包在 WPF 中执行导航?
I've just started a new project in which the presentation layer will be done by WPF and MVVM Light by GalaSoft.
I need a lot of views and it's not clear to me how to manage navigation through windows.
First of all, the templates offered in MVVM Light for creating a new "WPF MVVM View" create a new Window
that is not possible to use for navigation by frame (I mean, by putting a frame in mainView
and changing the source path to navigate).
Do I simply have to change Window
to Page
for all the views I create using templates?
Or is there a different way to perform navigation in WPF with the MVVM Light toolkit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常使用
ContentControl
来显示动态内容。它的Content
属性通常绑定到父ViewModel
中的CurrentViewModel
属性,并且DataTemplates
用于告诉 WPF如何绘制子ViewModels
。要更改视图,只需更改父
ViewModel
中的CurrentViewModel
属性即可。您可以在 我的这篇文章
I usually use a
ContentControl
to display dynamic content. It'sContent
property is usually bound to aCurrentViewModel
property in the parentViewModel
, andDataTemplates
are used to tell WPF how to draw the childViewModels
.To change views, simply change the
CurrentViewModel
property in the parentViewModel
You can find an example at this article of mine
最后我就按照这个方法做了。
遵循o_q的想法,我创建了NavigationWindow作为MainWindow,并将所有视图更改为页面。
然后,我创建了一个接口和一个使用导航的类:
然后,在 viewModelLocator 中,我创建了所有需要存储视图路径的 const 字符串:
在 App.cs 中,在 Application_Startup 事件处理程序中,在 Unity IoC 的帮助下,我注册了 NavigationService 的单例:
现在,在我的 ViewModelLocator 中,我可以注册一条“Galasoft”消息来捕获所有事件并导航到页面;在构造函数中我有:
通过这种方式,我让所有 viewModel 保持“无知”......它们对导航一无所知,而且我没有后面的代码。
如果我需要使用视图中的按钮进行导航,我可以从连接的 viewModel 解析 NavigationService 并导航到我需要的页面。
而且,最重要的是,它有效!
Eventually I did it this way.
Following the idea of o_q, I created NavigationWindow as MainWindow and changed all the the views to page.
Then, I created an inteface and a class which using Navigation:
Then, in viewModelLocator I created all the const string nedded to store the paths to my views:
In App.cs, in the Application_Startup event handler, with the help of Unity IoC I registered a singleton of NavigationService:
Now, in my ViewModelLocator, I can register a "Galasoft" message to catch all the events and navigate to a page; in the constructor I have:
In this way I keep all the viewModels "ignorant"... they don't know anything about navigation, plus I don't have code behind.
If I need to navigate by using a button from a view I can resolve NavigationService from the connected viewModel and navigate to the Page I need.
And, most important thing, it works!
对于可导航的应用程序,您希望启动视图是
NavigationWindow
而不是Window
代码隐藏:
MVVM Light 视图模板使用
Window
code>,但正如您所猜测的,您可以更改它。如果您希望能够导航至此视图或从此视图中导航出来,请将其设为页面
。这是您导航的方式:
代码隐藏:
For a navigable application, you'll want your start up view to be a
NavigationWindow
instead of aWindow
Code behind:
The MVVM Light view templates use
Window
, but as you have guessed, you can just change it. If you want to be able to navigate to and from this view, make it aPage
.This is how you navigate:
Code Behind: