[2 [2
InotifyPropertyChanged 对于数据绑定和属性更改通知至关重要,但是手动实现可能很麻烦。 而简化的语法,例如
将是理想的,它不是内置在C#中。 让我们探索简化过程的方法。 一种方法涉及一个带有通用
公共类数据:inotifyPropertychanged
{
受保护的虚拟void onProperTychanged(String propertyName);
受保护的bool setField
这降低了属性声明样板。 c#5的
属性进一步简化了以下简化:
受保护的bool setfield
public class Data : INotifyPropertyChanged
{
protected virtual void OnPropertyChanged(string propertyName);
protected bool SetField(ref T field, T value, string propertyName);
public string Name
{
get { return name; }
set { SetField(ref name, value, "Name"); }
}
// ... other properties
}
事件完全升高。 对于大型项目来说,这是一个有力的选择。 手动优化(使用基类)和自动代码生成之间的选择取决于项目大小和对外部依赖关系的偏爱。 免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3