在应用程序中管理多个WPF视图 [英] Managing multiple WPF views in an application

查看:89
本文介绍了在应用程序中管理多个WPF视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经看到WPF中存在导航服务,用于维护通过应用程序的流程.对于后退和前进类型功能,我并不是真正的市场.我只是在寻找一种在按下特定视图上的按钮时在视图之间进行切换的好方法.我正在使用MVVM,所以不确定是否可以让App.xaml.cs包含每个View或ViewModel的副本,并让ViewModel命令调用App.xaml.cs进行切换.也许我应该只处理按钮上的Click事件并做一些this.Close(); NewWindow.Show();.
与WPF中的许多事情一样,尽管可能有几个简单的解决方案,但对我来说这不是很直观.

So I've seen that a Navigation Service exists in WPF for maintaining a flow through an application. I'm not really in the market for the back and forward type functionality. I'm just looking for a good way to switch between Views when a button on a particular view is pressed. I'm using MVVM, so I'm not sure if I can let the App.xaml.cs possibly contain a copy of each View or ViewModel and let a ViewModel command call into App.xaml.cs to do the switching. Perhaps I should just handle the Click event on the button and do some flavor of this.Close(); NewWindow.Show();.
As with many things in WPF, this one is not intuitive to me although there probably are a couple simple solutions.

谢谢!

推荐答案

我创建了一个窗口加载器"类,该类在应用程序启动时会实例化.窗口加载器有一个字典,用于维护视图模型类型和视图类型的关联.它还有一个方法,该方法采用一个视图模型实例,根据视图模型的类型解析视图,实例化视图,将视图的数据上下文设置为视图模型,然后对其进行显示.窗口加载器还会在视图模型中注册一个事件,该事件在窗口要关闭时引发.

I created a "window loader" class that the app class instantiates when the application starts. The window loader has a dictionary that maintains an association of view model types and view types. It also has a method that takes a view model instance, resolves the view based on the view model's type, instantites the view, sets the view's datacontext to the view model then shows it. The window loader also registers for an event in the view model which is raised when the window wants to close.

窗口加载器实现IWindowLoader接口,并在每个视图模型中保留对其的引用(当窗口加载器实例化视图模型时,它会将自身分配给视图模型中的公共IWindowLoader属性).因此,任何视图模型都可以导致显示任何其他视图模型,而无需了解视图,也无需自己进行显示.而且,可以轻松地模拟窗口加载器以进行测试.

The window loader implements an IWindowLoader interface and a reference to it is kept in each view model (when the window loader instantiates a view model it assigns itself to a public IWindowLoader property in the view model). So any view model can cause any other view model to be shown without knowing about views and without doing the showing itself. Also, the window loader can easily be mocked for testing.

当我经历与您现在相同的过程时,我发现了许多相同基本模式的示例.我刚刚结束了自己的滚动.

When I went through the same process you are now, I found lots of examples of this same basic pattern. I just ended up rolling my own.

这篇关于在应用程序中管理多个WPF视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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