C# 我得到一个;CopyState中的编辑级别不匹配”;CSLA中的错误取决于执行BeginEdit的位置
我目前正在使用CSLA 4.5,遇到了一个错误 CopyState中的编辑级别不匹配 奇怪的是,如果我在一段代码中对业务对象执行C# 我得到一个;CopyState中的编辑级别不匹配”;CSLA中的错误取决于执行BeginEdit的位置,c#,wpf,.net-4.5,undo-redo,csla,C#,Wpf,.net 4.5,Undo Redo,Csla,我目前正在使用CSLA 4.5,遇到了一个错误 CopyState中的编辑级别不匹配 奇怪的是,如果我在一段代码中对业务对象执行BeginEdit()调用,它没有问题,但是如果我将它放在弹出窗口逻辑中,它会抛出这个异常,我不知道为什么。奇怪的关键在于它是同一个对象: MainControl.xaml.cs: public void ShowPopup() { // ... var target = ...; //target.BeginEdit(); <== No
BeginEdit()
调用,它没有问题,但是如果我将它放在弹出窗口逻辑中,它会抛出这个异常,我不知道为什么。奇怪的关键在于它是同一个对象:
MainControl.xaml.cs
:
public void ShowPopup()
{
// ...
var target = ...;
//target.BeginEdit(); <== No problem if I uncomment this
var window = new PopupWindow
{
DataContext = new ConfirmationViewModel(target),
IsApplyCancel = true,
Owner = this,
};
window.Show();
}
public class PopupWindow : Window
{
private static void OnIsApplyCancelChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var window = sender as Window;
if (window != null)
{
var context = window.DataContext as CSLAViewModelBase;
if (context != null)
{
var target = context.Target;
target.BeginEdit(); // EXCEPTION!!!
}
}
}
}
public class ConfirmationViewModel : CSLAViewModelBase
{
public ConfirmationViewModel(BusinessBase target)
: base(target) { ... }
}
确认视图模型.cs
:
public void ShowPopup()
{
// ...
var target = ...;
//target.BeginEdit(); <== No problem if I uncomment this
var window = new PopupWindow
{
DataContext = new ConfirmationViewModel(target),
IsApplyCancel = true,
Owner = this,
};
window.Show();
}
public class PopupWindow : Window
{
private static void OnIsApplyCancelChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var window = sender as Window;
if (window != null)
{
var context = window.DataContext as CSLAViewModelBase;
if (context != null)
{
var target = context.Target;
target.BeginEdit(); // EXCEPTION!!!
}
}
}
}
public class ConfirmationViewModel : CSLAViewModelBase
{
public ConfirmationViewModel(BusinessBase target)
: base(target) { ... }
}
数据绑定UI通常查找IEditableObject,并在对象绑定到表单时自动调用它。该接口不知道n级撤销,所以如果Csla检测到对这些方法的多次调用,它会抛出一个异常。您可能不需要在对话框中使用BeginEdit,只需要在将对象从窗体中解除绑定后考虑Cancel或EndEdit 我相信ViewModelBase也能为您做到这一点