我们有一个类ModelBase
,它定义了PropertyChangedEventHandler PropertyChanged
。然后,我们继承这个类,并使用一种装饰器模式,为它添加更多功能 - 过滤,插入等。我们将当前对象包装到另一个中,每次我们添加东西并复制所有属性,所以我们可以在下一堂课中使用它们。如何将EventHandler复制到同一层次结构的另一个类
public class ModelBase<T> : IModel<T> where T : IView, new()
{
...
...
public event PropertyChangedEventHandler PropertyChanged;
protected internal void RaisePropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ViewModel : ModelBase<Grid>, IViewModel
{
protected internal ViewModel(iewModel viewModel)
: base(viewModel)
{
// Copy all the properties of viewModel to the ones of this instance
...
...
PropertyChanged = viewModel.PropertyChanged;
}
}
的问题是,试图复制时PropertyChanged
事件处理程序编译器会引发错误... can only appear on the left hand side of += or -= operator...
。
有没有办法如何将PropertyChanged
属性复制到下一个装饰器isntance?
见 - http://stackoverflow.com/questions/660480 /确定列表事件处理程序绑定到事件 - 这是你想要做的? – ChrisF
这听起来像个糟糕的主意。你确定你想克隆事件处理程序吗? – SLaks
你做错了什么。如果底层对象是一个“活的”对象,也就是说,它的属性可能会发生变化,并且有事件的订阅者,那么您无法使用一次性副本:您需要捕获底层对象中的更改并反映他们在包装对象中。但是,如果您的底层对象只包含数据,则必须没有事件订阅者。 – Vlad