执行 RelayCommand WPF 之前的 UI 验证
我是 WPF 新手,我尝试使用 RelayCommand< 实现演示应用程序/a>.
我的问题是这样的:
如果我想执行一个命令,必须询问用户是否确定要先执行它 - 最好的方法是什么? 我需要一句“你确定吗?”弹出消息框。然而,该命令是在视图模型上执行的,当然我不想在那里弄乱 GUI。
谢谢
I'm new to WPF and I tried to implement a demo application with RelayCommand.
My question is this:
If i want to execute a command which has to ask the user if he's sure he wants to execute it first - what's the best way to do it?
I need an "are you sure?" messagebox to pop. However, the command is executed on the viewmodel, and of course I don't want to mess with GUI there.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我处理这个问题的方法是拥有一个在 IOC 中注册并可供 ViewModel 使用的
IDialogService
接口。然后,该服务提供与“用户”交互的各种方式。因此,您可以有一个ConfirmMessage 方法,该方法根据用户接受对话框返回true 或false。
然后,对于单元测试,您可以有一个不同的
IDialogService
实现,允许您在测试时向 ViewModel 提供预设响应。The way I deal with this is to have an
IDialogService
interface that is registered in your IOC and available to your ViewModels.The service then provides various ways of interacting with the "user". So you could have a ConfirmMessage method, that returns true or false based on the user accepting a dialog.
Then for unit testing say, you can have a different
IDialogService
implementation that allows you to feed canned responses to your ViewModel when under test.我自己也遇到过这个。我正在使用 MVVM Light,并使用 Messenger 来完成此任务。
我让我的 ViewModel 发送一个
GetConfirmationMessage
,我已在代码隐藏中注册了该消息。在 GetConfirmationMessage 的处理程序中,我弹出了对话框并获取了结果。如果用户单击“确定”,我就会发送一条ConfirmationReceived
消息,该消息由 ViewModel 处理以进行适当的更新。I just ran into this myself. I'm using MVVM Light, and I used the Messenger to accomplish this.
I had my ViewModel send a
GetConfirmationMessage
, which I had registered in the code-behind. Within the handler forGetConfirmationMessage
, I popped up the dialog box and got the reuslts. If the user clicked on OK, I then sent aConfirmationReceived
message, which was handled by the ViewModel to do the appropriate updates.