C# IDataErrorInfo与MVVM轻消息一起重新连接
对于我正在进行的项目,我使用IDataErrorInfo对用户输入进行验证。我还使用MVVM轻消息传递。但是,似乎错误处理会重新附加到每个ShowDialog上,这会导致验证多次发生,并且每次显示对话框时都会堆积起来。ViewModel在打开/关闭期间保留,不会重新创建 查看文本框 ViewModel RequestInputViewModel 视图模型2 RequestLPNViewModel是在viewmodel 2的构造函数中创建的C# IDataErrorInfo与MVVM轻消息一起重新连接,c#,wpf,mvvm,mvvm-light,idataerrorinfo,C#,Wpf,Mvvm,Mvvm Light,Idataerrorinfo,对于我正在进行的项目,我使用IDataErrorInfo对用户输入进行验证。我还使用MVVM轻消息传递。但是,似乎错误处理会重新附加到每个ShowDialog上,这会导致验证多次发生,并且每次显示对话框时都会堆积起来。ViewModel在打开/关闭期间保留,不会重新创建 查看文本框 ViewModel RequestInputViewModel 视图模型2 RequestLPNViewModel是在viewmodel 2的构造函数中创建的 Application.Current.Dispatch
Application.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send<ShowDialogMessageBase, MainView>(new ShowDialogMessage<RequestInputDialog>(this, RequestInputViewModel, InputEntered))));
调试输出中的输出
Pannenkoeken:2
Pannenkoeken:4
Pannenkoeken:6
Pannenkoeken:8
我不希望人数增加。我希望是2号的4倍。错误绑定似乎是在创建对话框时附加的,但在关闭对话框时不会释放。是的,int在打开时被重置:因为每次创建对话框时我都选择了重新创建viewmodel的对话框行为。也许更好的做法是防止将来使用旧数据/以前的数据出现任何“怪异”行为 viewmodel没有以这种方式表示任何状态
private string lpn;
public string LPN
{
get { return lpn; }
set
{
lpn = value; RaisePropertyChanged("LPN");
}
}
public string Error { get; private set; }
public string this[string propertyName]
{
get
{
string errorMsg = string.Empty;
if (propertyName.Equals("LPN"))
{
pannenkoek++;
if (!string.IsNullOrEmpty(lpn))
{
if (!LicensePlateNumber.IsValidLPN(lpn))
{
errorMsg = XmlTextProvider.GetHeader("LPNInvalid");
}
else if (!someManager.CanAddBag(LicensePlateNumber.Parse(lpn)))
{
errorMsg = XmlTextProvider.GetHeader("LPNDuplicate");
}
}
}
Error = errorMsg;
return Error;
}
}
private void Close(bool dialogResult)
{
System.Diagnostics.Debug.WriteLine(string.Format("Pannenkoeken: {0}", pannenkoek));
// Notify the view to close the dialog
Messenger.Default.Send<CloseWindowMessage, RequestInputDialog>(new CloseWindowMessage(this, dialogResult));
}
Application.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send<ShowDialogMessageBase, MainView>(new ShowDialogMessage<RequestInputDialog>(this, RequestInputViewModel, InputEntered))));