创建MVVM友好战略对话 [英] Creating an MVVM friendly dialog strategy

查看:272
本文介绍了创建MVVM友好战略对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建用于处理弹出形式使用我的整个应用程序的任何一部分的战略。我的理解到目前为止,我需要一个用户控件在我的主窗口的根源。这将被绑定到自己的视图模型将处理在应用程序中发送的消息。

I'm trying to create a strategy for handling popup forms for use throughout any part of my application. My understanding so far is that I will need a single UserControl in the root of my MainWindow. This will be bound to its own ViewModel which will handle the messages that are sent within the app.

我使用MVVM光,我是相当新的使者类。

I'm using MVVM Light, and I'm fairly new to the Messenger class.

想象一下,一个主/详细信息方案,其中一个列表中的对象包含一个的ListBox 之内。选择这些项目中的一个,然后点击一个编辑按钮将显示一个用户控件覆盖整个屏幕。然后,用户可以编辑选定的项目,然后单击确定以提交更改。

Imagine a Master/Details scenario, where a list a objects are contained within a ListBox. Selecting one of these items and clicking an Edit button would display a UserControl which covers the whole screen. The user can then edit the selected item, and click OK to commit the change.

我要的用户控件在打开是通用中,我可以扔任何(可能是一个视图模型)......它呈现的方式通过的DataTemplate 视图模型和处理所有的对象更改。单击确定将回调发送级别和持续的变化了。

I want the UserControl that is opened to be "generic" in a way that I can throw any (probably a ViewModel) at it... for it to render the ViewModel via a DataTemplate and handle all the object changes. Clicking OK will callback to the sending class and persist the change as before.

一些情况下,这将是有用的是...

Some situations where this would be useful are...


  1. 没有需要用户输入显示错误消息(不是OK,关闭吧)

  2. 显示编辑表单的数据项

  3. 确认对话框(很像一个标准的MessageBox)

任何人都可以提供我怎么可能达致这任何code样?

Can anyone provide any code samples of how I might acheive this?

推荐答案

在设计与MVVM的UI目标是分开查看从视图模型的关切关注的问题。理想情况下,应视图模型不依赖于任何视图组件。然而,这是新娘和MVVM的另一个原则是,如你所愿,你应该设计应用程序。

When designing a UI with MVVM the goal is to separate the concerns of the View from the concerns of the ViewModel. Ideally, the ViewModel should not rely on any view components. However, this is the idal and another rule of MVVM is that you should design your application as you wish.

在提供表示对话框有两种不同的方法浮动角落找寻一个服务的区域:

In the area providing a service showing dialogs there are two different approaches floating arround:


  1. 在查看实施DialogService(例如,见<一href=\"http://geekswithblogs.net/lbugnion/archive/2011/04/13/deep-dive-mvvm-samples-mix11-deepdivemvvm.aspx\">http://geekswithblogs.net/lbugnion/archive/2011/04/13/deep-dive-mvvm-samples-mix11-deepdivemvvm.aspx样品03)。

  2. 实现,它没有连接到视图服务组件(例如参见<一个href=\"http://blog.roboblob.com/2010/01/19/modal-dialogs-with-mvvm-and-silverlight-4/\">http://blog.roboblob.com/2010/01/19/modal-dialogs-with-mvvm-and-silverlight-4/)

  1. Implementing the DialogService on the View (e.g. see http://geekswithblogs.net/lbugnion/archive/2011/04/13/deep-dive-mvvm-samples-mix11-deepdivemvvm.aspx Sample 03).
  2. Implementing a service component that does is not attached to the view (e.g. see http://blog.roboblob.com/2010/01/19/modal-dialogs-with-mvvm-and-silverlight-4/)

这两种方法都依赖定义该服务所提供的功能的界面上。那么该服务的实现注入到视图模型。

Both approaches rely on an interface that defines the functionality the service provides. The implementation for this Service is then injected into the ViewModel.

此外,执行这两种方法都有其特定的优点和缺点。

Also, do both approaches have their specific advantages and disadvantages.


  • 第一种方法的工作原理也以WP7,但是,它需要一个共同观点的基类,因为这包含视图服务的实现。

  • 第二种方法可以很好地用于Silverlight和WPF和appleals,因为它使从视图独立服务,并没有对视图的任何restictions。

另一种可能的解决方案是使用消息传递来显示的对话框。

Another possible solution is to use messaging to show the dialogs.

不管你的方法是使用尽量保持查看和使用模式的IOC(控制反转)的视图模型脱钩,即定义一个接口,使您可以使用不同的实现。要将服务绑定到视图模型利用注入式,即通过服务到视图模型的构造函数或通过设置一个属性。

Whatever approach you are using try to keep the View and the ViewModel de-coupled by using an IoC (inversion of control) pattern, i.e. define an interface so that you can use different implementations. To bind the services into the ViewModel use injection, i.e. passing the service into the constructor of the ViewModel or by setting a property.

这篇关于创建MVVM友好战略对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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