Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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模式,用于将中间用户输入从ViewModel传递到模型_C#_.net_Wpf_Mvvm - Fatal编程技术网

C# MVVM模式,用于将中间用户输入从ViewModel传递到模型

C# MVVM模式,用于将中间用户输入从ViewModel传递到模型,c#,.net,wpf,mvvm,C#,.net,Wpf,Mvvm,我是MVVM新手,正在使用MVVM轻型框架将WinForms项目转换为WPF。对MVVM的大多数介绍都强调业务模型不应该了解视图模型。因此,我正在修改我的业务模型,通过添加公共属性和属性更改事件来支持我的新视图模型 但是,当我只想获得我不打算保存在模型中的用户输入时,这会让人觉得很尴尬。在WinForms中,我会在我的业务模型中这样做: dlg.ShowDialog(); string someValue = dlg.SomeValue; // Use someValue in a calcu

我是MVVM新手,正在使用MVVM轻型框架将WinForms项目转换为WPF。对MVVM的大多数介绍都强调业务模型不应该了解视图模型。因此,我正在修改我的业务模型,通过添加公共属性和属性更改事件来支持我的新视图模型

但是,当我只想获得我不打算保存在模型中的用户输入时,这会让人觉得很尴尬。在WinForms中,我会在我的业务模型中这样做:

dlg.ShowDialog();
string someValue = dlg.SomeValue; 
// Use someValue in a calculation...
这真的是MVVM的诅咒吗:

window.ShowDialog();
string someValue = _ViewModelLocator.MyVm.SomeValue;
它使我不必在业务模型中为真正需要的局部变量创建公共属性

谢谢你的建议和见解。

这是我写的一篇文章(即对话)

我建议使用一个界面来包装您的用户交互逻辑。 利用具有代表的用户界面将提供面向对象的解决方案

思想过程是在没有用户干预的情况下对用户交互进行单元测试

此外,我在上添加了此发现实现。 我相信您要使用的dll上的类名叫做UserInteraction

public delegate MessageBoxResult RequestConfirmationHandler(object sender, ConfirmationInteractionEventArgs e);

public interface IConfirmationInteraction
{
    event RequestConfirmationHandler RequestConfirmation;
    MessageBoxResult Confirm();
}

public class ConfirmationInteraction : IConfirmationInteraction
{
    #region Events
    public event RequestConfirmationHandler RequestConfirmation;
    #endregion

    #region Members
    object _sender = null;
    ConfirmationInteractionEventArgs _e = null;
    #endregion

    public ConfirmationInteraction(object sender, ConfirmationInteractionEventArgs e)
    {
        _sender = sender;
        _e = e;
    }

    public MessageBoxResult Confirm()
    {
        return RequestConfirmation(_sender, _e);
    }

    public MessageBoxResult Confirm(string message, string caption)
    {
        _e.Message = message;
        _e.Caption = caption;
        return RequestConfirmation(_sender, _e);
    }
}
}

public class ConfirmationInteractionEventArgs : EventArgs
    {
        public ConfirmationInteractionEventArgs() { }

        public ConfirmationInteractionEventArgs(string message, string caption, object parameter = null)
        {
            Message = message;
            Caption = caption;
            Parameter = parameter;
        }

        public string Message { get; set; }
        public string Caption { get; set; }
        public object Parameter { get; set; }
    }