问题描述
我有一个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()功能:
-
我可能会收到一条消息,内容为您确定...吗?".以及两个按钮是"和全部不在我的XAML中的边框中",并在模板上创建一个触发器,以便根据名为 AreYourSureDialogueBoxIsVisible 的ViewModelProperty折叠/显示该触发器,然后在需要此对话框时框,将AreYourSureDialogueBoxIsVisible分配为"true",然后还通过DelegateCommand处理两个按钮,回到我的ViewModel中.
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中的触发器来处理此问题,以便Delete按钮实际上只是使某些Border元素与消息和按钮一起出现,而Yes按钮确实进行了删除.
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()编写几行代码的人来说.
Both solutions seem to be too complex for what used to be a couple lines of code with 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.
签出演示#11"MVVM中的数据验证"(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()功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!