简体   繁体   English

MVVM ViewModel用于查看通信

[英]MVVM ViewModel to View Communication

I am new to MVVM and working on an application, i want to achieve few things in my application 我是MVVM的新手,正在开发应用程序,我想在应用程序中实现几件事

  1. My viewmodel should be able to initiate a new view. 我的视图模型应该能够启动一个新视图。 scenario(a command is bind to a button and some process decide what to do and based on the result, i need to show View1 or View2) 场景(一个命令绑定到一个按钮,一些进程决定要做什么,并根据结果,我需要显示View1或View2)

  2. Upon a successfull operation my viewmodel should display a messagebox, if multiple views are open then message must prompt upon the right view(with which viewmodel is bind). 操作成功后,我的视图模型将显示一个消息框,如果打开了多个视图,则消息必须在正确的视图上提示(绑定了该视图模型)。

I want to provide some kind of notification from my view model to view. 我想从我的视图模型中提供某种通知以进行查看。 Kindly guide me in the right direction. 请指引我正确的方向。

Thanks 谢谢

  1. You should create a new View and Navigate to it. 您应该创建一个新的视图并导航到它。
  2. You can use messaging in MVVM Light framework. 您可以在MVVM Light框架中使用消息传递。 Send message from your ViewModel to View. 从ViewModel向View发送消息。 Examples: http://chriskoenig.net/2010/07/05/mvvm-light-messaging/ 示例: http//chriskoenig.net/2010/07/05/mvvm-light-messaging/

You might want to try out some of the many mvvm frameworks out there. 您可能想尝试其中的许多mvvm框架。 I personally like mvvm light because it works in silverlight and WPF, and it's easy to use http://mvvmlight.codeplex.com/ (no affiliation) 我个人喜欢mvvm light,因为它可以在Silverlight和WPF中使用,并且易于使用http://mvvmlight.codeplex.com/ (无隶属关系)

Here is a nice compare/contrast of some of the major frameworks: What framework for MVVM should I use? 这是对一些主要框架的很好的比较/对比: 我应该使用哪种MVVM框架?

Most of the frameworks have a messaging system that provides the ability to send updates between the view and the viewmodel as well as between viewmodels. 大多数框架都有一个消息传递系统,该系统提供了在视图和视图模型之间以及视图模型之间发送更新的功能。 Most of the frameworks also provide canned messages that handle MVVM messageboxs as well (I know MVVM Light does). 大多数框架还提供了可处理MVVM消息框的固定消息(我知道MVVM Light可以)。

To handle switching between views in WPF I use DataTemplates and Content controls 为了处理WPF中的视图之间的切换,我使用了DataTemplates和Content控件

In the view .xaml I add <ContentControl Content="{Binding ActiveViewModel}" /> and this is the space where the injected view will be displayed. 在.xaml视图中,添加<ContentControl Content="{Binding ActiveViewModel}" /> ,这是将显示注入的视图的空间。 The ActiveViewModel is the object for the viewModel that holds the selected viewModel. ActiveViewModel是保存所选viewModel的viewModel的对象。

In a ResourceDictionary I add something like: 在ResourceDictionary中,我添加如下内容:

  <DataTemplate DataType="{x:Type ViewModelNameSpace:ViewModelClassName}">
    <ViewNameSpace:ViewClasName/>
  </DataTemplate>

Finally in the ViewModel I set the ActiveViewModel property (that is setup to notify the UI of changes via INotifyPropertyChanged) to an instance of the viewModel I would like to use. 最后,在ViewModel中,将ActiveViewModel属性(通过INotifyPropertyChanged通过UI通知更改的设置)设置为我要使用的viewModel实例。

ActiveViewModel = new ViewModelClass();

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

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