在WPF中从ViewModel调用View的最佳实践 [英] Best practice for calling View from ViewModel in WPF

查看:809
本文介绍了在WPF中从ViewModel调用View的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找wpf项目的一个小示例,其中包含在视图之间导航的最佳实践.也许使用MVVM Light框架和NavigationService或ServiceLocator.而不是从ViewModel调用View,您该怎么做?您的方法如何?您有项目示例吗?

I am looking for an little example of wpf project where contains a best practice to navigate between views. Maybe with the framework MVVM Light and a NavigationService or ServiceLocator. Instead of calling View from ViewModel, how do you do that? How is your approach? Do you have a project example?

推荐答案

我赞成的一种方法是拥有一个带有内容控件的主窗口.其后是一个具有基于共享枚举的应用程序状态属性的视图模型.

A way I favour is to have a main window with a content control. Behind that is a view model with an application state property based upon a shared enumeration.

枚举旨在表示当前状态,但如果更简单,则将其视为要显示的视图.

The enumeration is intended to represent the current state but consider each to be the view you want displaying if that's easier.

使用绑定到此属性的模板选择器,我可以控制主窗口中内容控件中显示的视图.

Using a template selector bound to this property I control the view presented within the content control on my main window.

此方法要求您使用每个枚举值或要用作主要内容的视图来管理具有模板属性的模板选择器.但是模板只需要包含视图的实例,因此最少的XAML.

This approach requires you to manage a template selector with template properties for each enumeration value or view you want to be the main content. But the template only needs to include an instance of your view, so minimum XAML.

在后台,我使用MVVM光源来提供Viewmodel定位器数据源,从而使我的视图真正易于实例化.他们还使用单例视图模型,因此可以在两次使用之间管理状态.然后,我还使用消息传递框架将消息发送到我的主窗口视图模型,告知其现在要处于哪个状态.这意味着任何视图模型都可以发布消息以切换到另一种状态,而无需知道将要使用的视图,但是最终,主窗口视图模型可以管理该过渡.

In the background, I use MVVM light to provide a Viewmodel locator data source making my views really easy to instantiate. They also use a singleton viewmodel so state is managed in between uses. I then also use the messaging framework to send messages to my main window view model telling it which state I now want. This means any viewmodel can post a message to switch to another state, without knowing what view that will be, but ultimately the main window view model manages that transition.

很抱歉,这里没有代码示例,这是我已经做过很多次的概述,但是我没有一个清晰的示例可以分享.

Apologies for lack of code examples here, this is an outline of something I have done a number of times but I don't have a clean sample to share.

希望这足以解释我的方法,但是如果这没有意义或不适合您尝试做的事情,请随时发表评论.

This is hopefully enough to explain my approach, but feel free to comment if this doesn't make sense or fit what you're trying to do.

对于对话框...通常我会重用默认对话框,但是可以使用相同的原理.与其作为主窗口控件和VM,不如为对话框提供辅助控件,它们的工作原理相同,但可以根据请求显示其自身,并在完成后关闭.同样,mvvm light Messenger是您的朋友在这里.

For dialogs... Typically I have default dialogs I reuse, but the same principle can be used. Instead of being the main window control and VM, have a secondary one for dialogs which works the same but makes itself visible as requested and closes itself once complete. Again mvvm light messenger is your friend here.

您的消息中包含对话框状态",该对话框控制显示的视图.

Your message includes the dialog 'state' which controls the view presented.

这仅在您的设计一次只允许一个对话框的情况下起作用.

This would only work if your design will allow only one dialog at a time.

最后,对于标签...

Finally, for tabs...

我要看一下在主视图模型中有一个可观察到的状态枚举集合.然后通过绑定到该选项卡来构建选项卡.我没有做过什么,但这就是我要开始的地方. –

I'd look at having an observable collection of the state enumeration in the main view model. Then build the tabs by binding to that. Not something I've done but that is where I'd start. –

这篇关于在WPF中从ViewModel调用View的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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