wpf mvvm light - 发送带有回调的通知消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22903989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mvvm light -Sending Notification message with callback
提问by Eldho
I need the result of FolderBrowserDialog in my view-model,
我的视图模型中需要 FolderBrowserDialog 的结果,
CodeBehind.cs
代码隐藏文件
private static void SelectFolderDialog()
{
using (System.Windows.Forms.FolderBrowserDialog folderdialg = new System.Windows.Forms.FolderBrowserDialog())
{
folderdialg.ShowNewFolderButton = false;
folderdialg.RootFolder = Environment.SpecialFolder.MyComputer;
folderdialg.Description = "Load Images for the Game";
folderdialg.ShowDialog();
if (folderdialg.SelectedPath != null)
{
var notifypath = new GenericMessage<string>(folderdialg.SelectedPath);
Messenger.Default.Send(notifypath);
}
}
What i'm planning is , From View-model send a notification with callback to view , executing the FolderBrowserDialog return the Selected path back to the view model.
我的计划是,从视图模型向视图发送带有回调的通知,执行 FolderBrowserDialog 将选定的路径返回到视图模型。
How do i send notificationmessage with callback / NotificationWithAction using MVVM-Light . please help me with a sample as I'm new to Wpf and MVVM-Light.
我如何使用 MVVM-Light 发送带有回调/ NotificationWithAction 的通知消息。请帮助我提供示例,因为我是 Wpf 和 MVVM-Light 的新手。
Any Help is appreciated
任何帮助表示赞赏
回答by Steve
I was looking for almost the exact same thing except for with a SaveFileDialog. Here is what I came up with:
我正在寻找几乎完全相同的东西,除了SaveFileDialog. 这是我想出的:
Create a message class with an Action<string>property and a constructor with an Action<string>argument.
创建一个带有Action<string>属性的消息类和带有Action<string>参数的构造函数。
public class SelectFolderMessage
{
public Action<string> CallBack {get;set;}
public SelectFolderMessage(Action<string> callback)
{
CallBack = callback;
}
}
In your ViewModel class, pass in a method or lambda expression when you call Messenger.Default.Send. I set a property in my ViewModel class with the path returned by the view. I wrapped this inside the execute section of a RelayCommand. I bound the RelayCommandto a button in the view
在您的 ViewModel 类中,当您调用Messenger.Default.Send. 我使用视图返回的路径在我的 ViewModel 类中设置了一个属性。我把它包裹在一个RelayCommand. 我将 绑定RelayCommand到视图中的按钮
...
new RelayCommand(() =>
{
Messenger.Default.Send(new SelectFolderMessage(
(pathfromview) => { viewmodelproperty = pathfromview;}));
})
In your view code behind, create a method to handle the message and register the handler with the messenger service. Don't forget to unregister if this is not your main window.
在后面的视图代码中,创建一个方法来处理消息并将处理程序注册到信使服务。如果这不是您的主窗口,请不要忘记取消注册。
public MainWindow()
{
Messenger.Default.Register<SelectFolderMessage>(this, SelectFolderHandler);
}
private void SelectFolderHandler(SelectFolderMessage msg)
{
using (System.Windows.Forms.FolderBrowserDialog folderdialg = new System.Windows.Forms.FolderBrowserDialog())
{
folderdialg.ShowNewFolderButton = false;
folderdialg.RootFolder = Environment.SpecialFolder.MyComputer;
folderdialg.Description = "Load Images for the Game";
folderdialg.ShowDialog();
if (folderdialg.SelectedPath != null)
{
msg.CallBack(folderdialg.SelectedPath);
}
}
}
I came up with this idea reading Laurent Bugnion's Messenger article in MSDN Magazine: http://msdn.microsoft.com/en-us/magazine/jj694937.aspx
我在阅读 MSDN 杂志中 Laurent Bugnion 的 Messenger 文章时提出了这个想法:http: //msdn.microsoft.com/en-us/magazine/jj694937.aspx

