[英]MVVM pattern for deliving intermediate user input from ViewModel to Model
我是MVVM的新手,正在使用MVVM Light框架将WinForms项目转换为WPF。 MVVM的大多数介绍都强调,业务模型应该不了解视图模型。 因此,我正在通过添加公共属性和属性更改事件来修改我的业务模型以支持新的视图模型。
但是,当我只想获取不会保存在模型中的用户输入时,这会感到很尴尬。 在WinForms中,我会在我的业务模型中这样做:
dlg.ShowDialog();
string someValue = dlg.SomeValue;
// Use someValue in a calculation...
这对MVVM真的是一种厌恶吗?
window.ShowDialog();
string someValue = _ViewModelLocator.MyVm.SomeValue;
这使我不必在业务模型中为仅需要真正成为局部变量的地方创建公共财产。
感谢您的建议和见解。
这是我写的关于单元测试用户交互 (即对话框)的文章。
我建议使用界面来包装用户交互逻辑。 利用带有委托的用户界面将提供面向对象的解决方案。
思考过程是在无需用户干预的情况下对用户交互进行单元测试。
另外,我在Nuget上添加了此实现以便发现。 我相信您要使用的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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.