简体   繁体   English

在 ViewModel 中处理对话框的最佳方式是什么?

[英]What is the best way to deal with dialogs in a ViewModel?

Following-up on the IDialogService approach:跟进 IDialogService 方法:

What about the System.Windows.MessageBoxResult enum? System.Windows.MessageBoxResult枚举怎么样? Is the better approach to keep it out of the interface and only involve it in the implementation?将其保留在接口之外并仅将其包含在实现中是更好的方法吗?

Approach I chose for the System.Windows.MessageBoxResult enum :我为System.Windows.MessageBoxResult枚举选择的方法

I added an enum next to the IDialogInterface that covers Yes, NO, Ok, Cancel:我在 IDialogInterface 旁边添加了一个枚举,涵盖 Yes、NO、Ok、Cancel:

namespace Foo.Bar.Dialogs
{
    public enum DialogResult { Ok, Yes, No, Cancel }

    public interface IDialogService
    {
        void ShowErrorBox(string error_message);

        DialogResult ShowQuestionBox(string question_message);

        DialogResult ShowQuestionBox(string question_message, string caption);

        DialogResult ShowQuestionBox(string question_message, string caption, bool allow_cancel);

        DialogResult ShowQuestionBox(string question_message, string caption, bool allow_cancel, bool show_as_error);

        void ShowWarningBox(string message, string caption = "");

        void ShowInformationBox(string message);

        void ShowInformationBox(string message, string caption);
    }
}

Initial question:初始问题:

I am working on moving all the commands from my.asmx.cs file to the ViewModel of the main window of some application.我正在努力将所有命令从 my.asmx.cs 文件移动到某些应用程序的主要 window 的 ViewModel。

Now I have to figure out what to do with commands that ask for confirmation(s) from the user.现在我必须弄清楚如何处理要求用户确认的命令。

For now I'll just suck in the necessary types in my ViewModel to launch my dialog boxes directly.现在,我将在我的 ViewModel 中吸收必要的类型以直接启动我的对话框。 I'm pretty sure this is not the best or cleanest way to do this.我很确定这不是最好或最干净的方法。

I found this article with an interesting and cleaner approach.我发现这篇文章采用了一种有趣且更简洁的方法。 It uses an IDialogService interface:它使用 IDialogService 接口:

public interface IDialogService
{
    int Width { get; set; }
    int Height { get; set; }
    void Show(string title, string message, Action<DialogResult> onClosedCallback);
}

I also found this article which seems even better as it checks if the IDialogInterface is null before trying to use it:我还发现这篇 文章看起来更好,因为它在尝试使用之前检查 IDialogInterface 是否为 null:

private void PerformAddNewCustomer() 
{ 
    CustomerList.Add(new Customer { Name = "Name" + i }); 
    i++; 

    if (dialogService != null) 
    { 
        dialogService.Show("Customed added"); 
    } 
} 

Is this the best way to separate dialogs from the ViewModel, or is there an even better approach?这是将对话框与 ViewModel 分开的最佳方法,还是有更好的方法?

I would say the approach in the link you posted is a good one and also very widespread (source: me looking at code on the web;) ).我会说您发布的链接中的方法是一个很好的方法,而且非常普遍(来源:我正在查看 web 上的代码;))。 It's rather simple to use, it makes dialogs testable by using a dummy service in your unit tests and it simplifies the process of refactoring your dialogs.它使用起来相当简单,它通过在单元测试中使用虚拟服务使对话框可测试,并且简化了重构对话框的过程。

Personally, my DialogService method signature takes a list of Tuple<string, Action> and I create my dialog window's buttons based on that.就我个人而言,我的DialogService方法签名采用Tuple<string, Action> ,我基于它创建对话窗口的按钮。 It allows the ViewModel to request for some special functions from the dialog windows without having to add new methods to my service every time my needs are more than an Ok or YesNo message box (while I do have those as methods on their own for ease of use).它允许ViewModel从对话框 windows 中请求一些特殊功能,而不必在每次我的需求不仅仅是一个OkYesNo消息框时向我的服务添加新方法(虽然我确实将这些方法作为自己的方法以方便采用)。

So it's a solid approach and at this point I don't think you will find something that is flat out better, only something you might like more personally.所以这是一个可靠的方法,在这一点上我不认为你会找到更好的东西,只有你可能更喜欢的东西。

The article you found is a good approach to deal with dialog windows in MVVM and it is really reusable and with the DI it is easy to use in your views.您找到的文章是在 MVVM 中处理对话框 windows 的好方法,它确实可以重用,并且使用 DI 可以很容易地在您的视图中使用。

I did use something different, most of the time i use MVVM light toolkit and it has a messenger and i used the messenger to send a message to a separated class and then it opened for me the dialog i wanted to show, and the result is messaged back so i can act to the users choice.我确实使用了一些不同的东西,大部分时间我使用 MVVM light 工具包,它有一个信使,我用信使向一个单独的 class 发送消息,然后它为我打开了我想显示的对话框,结果是回复消息,以便我可以根据用户的选择采取行动。

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

相关问题 绘制对象并对其进行处理的最佳方法是什么? - What is the best way to draw objects and deal with them? 处理DBNull的最佳方法是什么? - What is the best way to deal with DBNull's 将事件传递给ViewModel的最佳方法是什么? - What's the best way to pass event to ViewModel? 在进行TDD时,处理转换方法的最佳方法是什么? - What is the best way to deal with conversion methods when doing TDD? 在策略模式中,处理共享行为的最佳方法是什么? - In the strategy pattern, what's the best way to deal with shared behaviour? 在C#中处理共享dll的最佳方法是什么? - What is the best way to deal with shared dlls in C#? 处理kludgy接口层次结构(MSHTML)的最佳方法是什么? - What is the best way to deal with kludgy interface hierarchies (MSHTML)? 什么是处理几列更新的最佳方法 - What's the best way to deal with the updating of several columns 在MVVMCross中将对象传递给“导航到”viewmodel的最佳方法是什么? - What is the best way to pass objects to “navigated to” viewmodel in MVVMCross? 什么是处理异常以及如何在asp.net中处理它们的最佳方法 - What's the best way to handle the exceptions and how to deal with them in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM