我有一个基本模型(BM)和一个派生模型(DM)。 当每个类的属性更改时,我都有一些属性需要设置IsChanged属性(希望仅在派生类中)。
我尝试了将[AddINotifyPropertyChangedInterface]放置在基类或派生类或这两者上的多种排列方式。
我的问题是IsChanged的处理。我似乎无法在所有情况下都将IsChanged正确设置为false(从我的代码)。当我单步执行代码时,DMinstance.IsChanged = false,不会转到我编写的代码,而是转到没有源代码的代码,我认为这是编织的代码。
我尝试将IsChanged添加到两个基类中,然后使用NEW关键字将该属性添加到派生类中。我还尝试过使基类中的IsChanged虚拟化,并在派生类中进行重写。似乎都不起作用。
如所描述的,在基本模型中带有虚拟IsChanged的版本。该版本已尝试使用所有3种组合AddINotifyPropertyChangedInterface
在派生类上
[PropertyChanged.AddINotifyPropertyChangedInterface]
public partial class BM
{
private bool vdb_is_changed;
[PropertyChanged.DoNotSetChanged]
public virtual bool IsChangedChanged
{
get => vdb_is_changed || (NameAndDescription?.IsChanged ?? false);
set
{
vdb_is_changed = value;
if (NameAndDescription != null)
NameAndDescription.IsChanged = value;
}
}
public NameAndDescription NameAndDescription
{
get; set;
}
// other auto properties
}
[PropertyChanged.AddINotifyPropertyChangedInterface]
public partial class DM: BM
{
[PropertyChanged.DoNotSetChanged]
override public bool IsChanged
{
get => base.IsChanged;
set => base.IsChangedChanged = value;
}
// other auto properities
}
第二版再次尝试了[PropertyChanged.AddINotifyPropertyChangedInterface]应用的所有3种情况
[PropertyChanged.AddINotifyPropertyChangedInterface]
public partial class BM
{
private bool vdb_is_changed;
[PropertyChanged.DoNotSetChanged]
public bool IsChangedChanged
{
get => vdb_is_changed || (NameAndDescription?.IsChanged ?? false);
set
{
vdb_is_changed = value;
if (NameAndDescription != null)
NameAndDescription.IsChanged = value;
}
}
public NameAndDescription NameAndDescription
{
get; set;
}
// other auto properties
}
[PropertyChanged.AddINotifyPropertyChangedInterface]
public partial class DM: BM
{
// other auto properities
}
第三版还尝试了[PropertyChanged.AddINotifyPropertyChangedInterface]应用的所有3种情况
[PropertyChanged.AddINotifyPropertyChangedInterface]
public partial class BM
{
private bool vdb_is_changed;
[PropertyChanged.DoNotSetChanged]
public bool IsChangedChanged
{
get => vdb_is_changed || (NameAndDescription?.IsChanged ?? false);
set
{
vdb_is_changed = value;
if (NameAndDescription != null)
NameAndDescription.IsChanged = value;
}
}
public NameAndDescription NameAndDescription
{
get; set;
}
// other auto properties
}
[PropertyChanged.AddINotifyPropertyChangedInterface]
public partial class DM: BM
{
[PropertyChanged.DoNotSetChanged]
public new bool IsChanged
{
get => base.IsChanged;
set => base.IsBaseChanged = value;
}
// other auto properities
}