Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvvm light-通过回调发送通知消息_C#_Wpf_Mvvm_Mvvm Light - Fatal编程技术网

C# mvvm light-通过回调发送通知消息

C# mvvm light-通过回调发送通知消息,c#,wpf,mvvm,mvvm-light,C#,Wpf,Mvvm,Mvvm Light,我需要视图模型中FolderBrowserDialog的结果 CodeBehind.cs private static void SelectFolderDialog() { using (System.Windows.Forms.FolderBrowserDialog folderdialg = new System.Windows.Forms.FolderBrowserDialog()) { folderdialg.ShowN

我需要视图模型中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);

            }
        }
我计划的是,从视图模型向视图发送一个带有回调的通知,执行FolderBrowserDialog将所选路径返回到视图模型

如何使用MVVM Light通过回调/NotificationWithAction发送notificationmessage。请帮助我一个样本,因为我是新的Wpf和MVVM轻


非常感谢您的帮助

除了SaveFileDialog之外,我一直在寻找几乎完全相同的东西。以下是我的想法:

创建具有Action属性的消息类和具有Action参数的构造函数

public class SelectFolderMessage
{
    public Action<string> CallBack {get;set;}
    public SelectFolderMessage(Action<string> callback)
    {
         CallBack = callback;
    }
}
在查看代码隐藏中,创建一个处理消息的方法,并向messenger服务注册处理程序。如果这不是您的主窗口,请不要忘记取消注册

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);
        }
    }
}

我在读Laurent Bugnon在MSDN杂志上的Messenger文章时想到了这个想法:

除了SaveFileDialog之外,我在寻找几乎完全相同的东西。以下是我的想法:

创建具有Action属性的消息类和具有Action参数的构造函数

public class SelectFolderMessage
{
    public Action<string> CallBack {get;set;}
    public SelectFolderMessage(Action<string> callback)
    {
         CallBack = callback;
    }
}
在查看代码隐藏中,创建一个处理消息的方法,并向messenger服务注册处理程序。如果这不是您的主窗口,请不要忘记取消注册

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);
        }
    }
}

我是在阅读劳伦特·布尼翁在MSDN杂志上的Messenger文章时想到这个主意的:

这是一个更好的解决方案。谢谢,如果它的接口驱动,我如何管理它呢?我也在考虑单元测试能力。请查看上面链接中文章的实现DialogService部分。Laurent展示了一个使用接口和单元测试的示例。2017年更新:同时在MVVMlight中有。消息通知MessageAction、NotificationMessageAction和NotificationMessageWithCallback。这是一个更好的解决方案。谢谢,如果它的接口驱动,我如何管理它呢?我也在考虑单元测试能力。请查看上面链接中文章的实现DialogService部分。Laurent展示了一个使用接口和单元测试的示例。更新2017:同时在MVVMlight中有。消息传递NotificationMessageAction、NotificationMessageAction和NotificationMessageWithCallback。