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

查看:31
本文介绍了在 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天全站免登陆