使用MVVM Light工具包显示对话框 [英] Show dialog with MVVM Light toolkit
问题描述
我有一个ViewModel需要显示一个模态窗口(使用 ShowDialog()
)在按钮上单击。 ViewModel捕获click命令,但我不想在ViewModel中执行 window.ShowDialog()
。我知道MVVM Light中有一个 DialogMessage
,但是它用于显示消息框,而不是WPF模态窗口。
I have a ViewModel that needs to show a modal window (using ShowDialog()
) on a button click. The ViewModel catches the click command, but I don't want to do window.ShowDialog()
within my ViewModel. I know there is a DialogMessage
in MVVM Light, but that is used to show message boxes, not WPF modal windows.
有关如何做的任何想法?
Any ideas on how to do this?
推荐答案
使用 Messenger
类。在查看
注册一个消息以显示窗口,然后当您需要显示它调用发送
方法 Messenger
class。
You should use Messenger
class. On the View
register a message to show window and then when you need to show it call Send
method of Messenger
class.
你可以这样做:
//do this in code behind file of your View
Messenger.Default.Register<string>(this, ShowWindow);
private void ShowWindow(string message)
{
// your logic here
}
// In the ViewModel
Messenger.Default.Send("Some text");
这篇关于使用MVVM Light工具包显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!