带有视图模型,参数和结果的Mvvm弹出窗口 [英] Mvvm popup window with view model, parameter and result

查看:110
本文介绍了带有视图模型,参数和结果的Mvvm弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了两天的时间来弄清楚。

I have spent two day trying to figure it out.

我实现了两种使用mvvm弹出窗口的方式

I have implemented two ways of working with mvvm popup windows

第一种用法的示例:

_childWindowController
                .ShowDialogWithResult<AddNationalityPopup,AddNationalityPopupModel, AddNationalityResult>(
                    (result, a) =>
                    {
                        if (a.DialogResult.HasValue && a.DialogResult.Value)
                        {
                            if (result.NationalityCountryId.HasValue)
                            {
                                Background.NationalityCountryId = result.NationalityCountryId.Value;
                                Background.NationalityDescription = result.NationalityDescription;
                            }
                        }
                    });

第二种方法:

var window = _childWindowController.CreateDialog<AddNationalityPopup>();

    window.Closed += (sender, args) =>
    {
        if (args.DialogResult.HasValue && args.DialogResult.Value)
        {
            var result = (AddNationalityResult)window.Result;
            if (result.NationalityCountryId.HasValue)
            {
                Background.NationalityCountryId = result.NationalityCountryId.Value;
                Background.NationalityDescription = result.NationalityDescription;
            }
        }
    };

    window.ShowDialog();

在第一种方法中,服务的用户应该知道视图的类型,视图模型和结果能够显示对话框

In the first approach user of the service should know the types of view , view model, and result to be able to show dialog

在第二个界面中进行了一些简化,但是我仍然必须知道使用哪种类型将结果强制转换为结果。

In the second one interface is simplified a bit, but I still had to know to what type cast the result before its usage.

您是否曾经遇到过使用视图模型显示对话框的问题?

Have you ever faced the problem of showing dialog with view model?

如何改善窗口服务的设计?

How to improve the design of the window service?

您能举一个很好的对话框服务实现示例吗?

Can you give an example of good implementation of the dialog service?

推荐答案

我建议您看看用户交互模式,因为它介绍了您可以采用多种方法来处理MVVM中的用户交互。使用互动服务是实施互动请求对象

I recommend you take a look at User Interaction Patterns, as it goes over the different approaches you can take to handling user interactions in MVVM. An alternative to using an interaction service is to implement an interaction request object.


在MVVM
模式中实现
简单用户交互的另一种方法是允许
的视图模型通过交互
请求对象以及视图中的行为
向视图本身直接向
进行交互请求。交互请求
对象封装了
交互请求的详细信息及其响应
,并通过
事件与视图进行通信。视图订阅了这些
事件,以启动交互的用户体验
部分。视图
通常会以
行为封装用户的交互
体验,该行为与
视图模型提供的
交互请求对象数据绑定。

Another approach to implementing simple user interactions in the MVVM pattern is to allow the view model to make interaction requests directly to the view itself via an interaction request object coupled with a behavior in the view. The interaction request object encapsulates the details of the interaction request, and its response, and communicates with the view via events. The view subscribes to these events to initiate the user experience portion of the interaction. The view will typically encapsulate the user experience of the interaction in a behavior that is data-bound to the interaction request object provided by the view model.

此方法提供了一种简单但灵活的
机制,可在视图
模型与视图之间保持
清晰的分离-它允许视图
模型封装应用程序的
表示逻辑,包括任何
所需的用户交互,而
则允许视图完全封装
的视觉方面相互作用。
视图模型的实现,
包括其与用户通过视图的预期交互
,可以很容易地测试
,并且UI设计师在选择
a时有很大的灵活性
如何通过使用不同的
行为来封装
的不同用户体验的
行为,在视图中的
中实现交互。

This approach provides a simple, yet flexible, mechanism that preserves a clean separation between the view model and the view — it allows the view model to encapsulate the application's presentation logic, including any required user interactions, while allowing the view to fully encapsulate the visual aspects of the interaction. The view model's implementation, including its expected interactions with the user through view, can be easily tested, and the UI designer has a lot of flexibility in choosing how to implement the interaction within the view via the use of different behaviors that encapsulate the different user experiences for the interaction.

有关如何实现此功能的示例,建议您查看 Prism 4库源代码及其示例。 Prism库通过 IInteractionRequest 接口和 InteractionRequest 类。 IInteractionRequest接口定义一个事件来启动交互,而视图中的行为绑定到该接口并订阅它公开的事件。

For an example of how implement this, I recommend you take a look at the Prism 4 library source code and its samples. The Prism library supports this pattern through the IInteractionRequest interface and the InteractionRequest class. The IInteractionRequest interface defines an event to initiate the interaction, while behaviors in the view bind to this interface and subscribe to the event that it exposes.

您可以利用这些类和在 Microsoft.Practices.Prism.Interactivity 程序集中定义的接口,或使用这些类型作为实现对话框服务的基础。

You could utilize the classes and interfaces defined in the Microsoft.Practices.Prism.Interactivity assembly, or use these types as a basis for implementing your dialog service.

这篇关于带有视图模型,参数和结果的Mvvm弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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