您是如何在 MVVM 中成功实现 MessageBox.Show() 功能的? [英] How have you successfully implemented MessageBox.Show() functionality in MVVM?

查看:38
本文介绍了您是如何在 MVVM 中成功实现 MessageBox.Show() 功能的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序它在 ViewModel 中调用 MessageBox.Show() 方法(检查用户是否真的想删除).这确实有效,但是违背了 MVVM 的原则,因为 ViewModel 不应该明确地确定 View 上发生的事情.

I've got a WPF application which calls MessageBox.Show() way back in the ViewModel (to check if the user really wants to delete). This actually works, but goes against the grain of MVVM since the ViewModel should not explicitly determine what happens on the View.

所以现在我在想如何在我的 MVVM 应用程序中最好地实现 MessageBox.Show() 功能,选项:

So now I am thinking how can I best implement the MessageBox.Show() functionality in my MVVM application, options:

  1. 我可以有一条消息,文本为你确定......?"连同两个按钮 Yes 和 No 都在我的 XAML 的边框中,并在模板上创建一个触发器,以便它基于名为 AreYourSureDialogueBoxIsVisible 的 ViewModelProperty 折叠/可见,然后在我需要此对话时框,将 AreYourSureDialogueBoxIsVisible 指定为true",并通过 DelegateCommand 处理这两个按钮,返回我的 ViewModel.

  1. I could have a message with the text "Are you sure...?" along with two buttons Yes and No all in a Border in my XAML, and create a trigger on the template so that it is collapsed/visible based on a ViewModelProperty called AreYourSureDialogueBoxIsVisible, and then when I need this dialogue box, assign AreYourSureDialogueBoxIsVisible to "true", and also handle the two buttons via DelegateCommand back in my ViewModel.

我也可以尝试用 XAML 中的触发器来处理这个问题,这样删除按钮实际上只是让一些边框元素出现,其中包含消息和按钮,而是按钮进行实际删除.

I could also somehow try to handle this with triggers in XAML so that the Delete button actually just makes some Border element appear with the message and buttons in it, and the Yes button did the actually deleting.

对于过去使用 MessageBox.Show() 的几行代码来说,这两种解决方案似乎都太复杂了.

您通过哪些方式在 MVVM 应用程序中成功实施了对话框?

推荐答案

在你提到的两个中,我更喜欢选项 #2.页面上的删除按钮只会显示确认删除对话框".确认删除对话框"实际上启动了删除.

Of the two you mention, I prefer option #2. The Delete button on the page just makes the "Confirm Delete Dialog" appear. The "Confirm Delete Dialog" actually kicks off the Delete.

您是否查看过 Karl Shifflett 的 WPF 业务线幻灯片和演示?我知道他会做这样的事情.我会尽量记住在哪里.

Have you checked out Karl Shifflett's WPF Line Of Business Slides and Demos? I know he does something like this. I'll try to remember where.

查看演示 #11MVVM 中的数据验证"(EditContactItemsControlSelectionViewModel.DeleteCommand).Karl 从 ViewModal 调用一个弹出窗口(什么!?:-).我其实更喜欢你的想法.似乎更容易进行单元测试.

Check out Demo #11 "Data Validation in MVVM" (EditContactItemsControlSelectionViewModel.DeleteCommand). Karl calls a popup from the ViewModal (What!? :-). I actually like your idea better. Seems easier to Unit Test.

这篇关于您是如何在 MVVM 中成功实现 MessageBox.Show() 功能的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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