如何提高派生类中已更改的属性? [英] How to Raise Property Changed in Derived Classes?

查看:132
本文介绍了如何提高派生类中已更改的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在B类中为SomeProperty提高PropertyChanged?

此示例无法编译,因为无法以这种方式访问​​PropertyChanged ...

This example does not compile since PropertyChanged is not accessible this way...

public class A : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
}

public class B : A
{
    private object _someProperty;

    public object SomeProperty
    {
        get => _someProperty;
        set
        {
            _someProperty = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SomeProperty)))
        }
    }
}

推荐答案

解决方案1:

您可以使用此RaisePropertyChangedExtension:

public static class RaisePropertyChangedExtension
{
    public static void RaisePropertyChanged(this INotifyPropertyChanged @this, [CallerMemberName] string propertyName = null)
    {
        var declaringType = @this.GetType().GetEvent(nameof(INotifyPropertyChanged.PropertyChanged)).DeclaringType;
        var propertyChangedFieldInfo = declaringType.GetField(nameof(INotifyPropertyChanged.PropertyChanged), BindingFlags.Instance | BindingFlags.NonPublic);
        var propertyChangedEventHandler = propertyChangedFieldInfo.GetValue(@this) as PropertyChangedEventHandler;
        propertyChangedEventHandler?.Invoke(@this, new PropertyChangedEventArgs(propertyName));
    }
}

赞:

public class B : A
{
    private object _someProperty;

    public object SomeProperty
    {
        get => _someProperty;
        set
        {
            _someProperty = value;
            this.RaisePropertyChanged();
        }
    }
}

我认为这是到目前为止我所知道的最好的解决方案.

In my opinion this is the best solution I know so far.

缺点是您可以从另一个这样的类中提高PropertyChanged:

Disadvantage is that you're able to raise PropertyChanged from another class like this:

public class C
{
    public C(B b)
    {
        b.RaisePropertyChanged(nameof(b.SomeProperty));
    }
}

以这种方式从其他类中提高PropertyChanged并不是一个好习惯,所以我对此并不担心.

It's not good practise to raise PropertyChanged from other classes this way, so i'm not concerned by this disadvantage.

此解决方案的灵感来自 Thomas Levesque 在这里的答案:

This solution is inspired by Thomas Levesque's answer here: Simple small INotifyPropertyChanged implementation

您可以在基类A中创建受保护的RaisePropertyChanged:

You can create a protected RaisePropertyChanged in the base class A:

public class A : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

并在派生类B中调用该方法:

And call the method in the derived class B:

public class B : A
{
    private object _someProperty;

    public object SomeProperty
    {
        get => _someProperty;
        set
        {
            _someProperty = value;
            RaisePropertyChanged();
        }
    }
}

缺点是,您必须为正在创建的每个新基类实现RaisePropertyChanged方法,这样可以避免解决方案1 ​​所具有的缺点.

Disadvantage is that you have to implement the RaisePropertyChanged method for each new base class you're creating on the opposite you avoid the disadvantage that Solution 1 had.

这篇关于如何提高派生类中已更改的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆