代码之家  ›  专栏  ›  技术社区  ›  Alan Wayne

如何将inotifypropertychanged添加到包含泛型类型的类中?

  •  -1
  • Alan Wayne  · 技术社区  · 5 年前

    wpf c泛型

    我对泛型很陌生。我是 尝试 要为具有私有setter的实体创建编辑器,请执行以下操作:

    public class EditorBase<T>  where T: class, new() 
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    }
    
    
    
    public class AccountEditor<T> :  EditorBase<T> where T : class, new()
        {
            public AccountEditor()
            {
                bR = new BillingRepository();
                pt = new ViewPatient();
            }
    
            public viewName Name { get; private set; }
            public ViewPatient pt { get; private set; }
    
            public virtual async void SetAccountAsync(viewName _name)
            {
                Name = _name;
                if (_name == null)
                    pt = new ViewPatient();
                else
                {
                    pt = await bR.GetPersonByNameAsync(_name.lastName, _name.firstName, DateTime.Parse(_name.birthDate));
                    RaisePropertyChanged();
                }
            }
    
    
            private readonly BillingRepository bR;
    
            private string _mi;
            public string mi
            {
                get { return pt.mi; }
                set { if (pt.mi == value) return; pt.SetMi(value); RaisePropertyChanged(); }
    }
    

    我使用的是 用于绑定的编辑器。我的问题是在 编辑类。

    如何将接口inotifypropertychanged添加到类editorbase声明中?

    此语法失败: editorBase,其中t:Class,new():inotifyPropertyChanged

    提前谢谢你的帮助。

    1 回复  |  直到 5 年前
        1
  •  2
  •   Blindy    5 年前

    此语法失败:editorbase,其中t:class,new():inotifyPropertyChanged

    正确的语法是 EditorBase<T> : INotifyPropertyChanged where T:class, new()

    由于实体具有私有setter,因此它们不能实现inotifypropertychanged本身

    我真的不知道你是什么意思。任何类都可以声明它是否实现了任何接口,而不管其属性设置器使用什么可见性。