c# - 带有 View 模型、参数和结果的 Mvvm 弹出窗口

标签 c# silverlight design-patterns mvvm

我花了两天时间想弄明白。

我已经实现了两种使用 mvvm 弹出窗口的方法

第一个方法的用法示例:

_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();

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

第二个界面稍微简化了一点,但我仍然必须知道在使用之前将结果转换为什么类型。

您是否遇到过使用 View 模型显示对话框的问题?

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

您能举例说明对话服务的良好实现吗?

最佳答案

我建议你看看User Interaction Patterns ,因为它介绍了您可以采用的不同方法来处理 MVVM 中的用户交互。使用 interaction service 的替代方法是实现一个interaction request object .

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.

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 library源代码及其示例。 Prism 库通过 IInteractionRequest 支持这种模式接口(interface)和 InteractionRequest类(class)。 IInteractionRequest 接口(interface)定义了一个事件来启动交互,而 View 中的行为绑定(bind)到这个接口(interface)并订阅它公开的事件。

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

关于c# - 带有 View 模型、参数和结果的 Mvvm 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6275352/

相关文章:

c# - 链式 LINQ 语句的效率如何?

flash - 将焦点传递给 Chrome 和 Safari 中的 Silverlight/Flash 对象

c# - System.Core.ni.dll 中发生类型 'System.ArgumentOutOfRangeException' 的异常,但未在用户代码中处理

silverlight - Silverlight 中是否有全局焦点更改事件?

单例设计模式中的Java静态对象范围不会抛出空指针异常

windows-10 - 检查是否授予位置权限

c# - System.ObjectDisposedException : Cannot access a closed Stream

java - Iterable接口(interface)的目的

javascript - 等待异步事件处理程序的设计模式

c# - 使用 TempData 几个