简体   繁体   English

从抽象类继承静态事件的 INotifyPropertyChanged

[英]Inherit INotifyPropertyChanged for Static Event from abstract Class

I'm trying to implement a number of Abstraction Layers for various pieces of hardware, and I've found I've implemented the same function, NofityStaticPropertyChanged (from here ) about 5 times.我正在尝试为各种硬件实现多个抽象层,我发现我已经实现了大约 5 次相同的功能, NofityStaticPropertyChanged (来自此处)。

    /// <summary>
    /// A slightly different implementation of Static Property Changed, from here:
    /// https://stackoverflow.com/a/42111290/2444435
    /// </summary>
    public static event PropertyChangedEventHandler StaticPropertyChanged;

    protected static void NotifyStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }

I'd like to have all of these classes inherit from an abstract class AbstractionBase to avoid reusing code.我想让所有这些类都继承自abstract class AbstractionBase以避免重用代码。

I've read elsewhere as to how to inherit this sort of function for non-static members, but I'm never actually instantiating these abstraction layers/factories.我在别处读过关于如何为非静态成员继承这种函数的文章,但我从来没有真正实例化这些抽象层/工厂。

I'm not sure this is actually possible, since static properties cannot be inherited.我不确定这实际上是否可行,因为不能继承静态属性。 I feel like it ought to be though, since it's basically a function, not a property?我觉得它应该是,因为它基本上是一个函数,而不是一个属性? My current attempt is thus:我目前的尝试是这样的:

public abstract class AbstractionBase : INotifyPropertyChanged
{
    //THIS IS NOT WORKING!
    #region INofity Declarations
    /// <summary>
    /// A slightly different implementation of Static Property Changed, from here:
    /// https://stackoverflow.com/a/42111290/2444435
    /// </summary>
    public static event PropertyChangedEventHandler StaticPropertyChanged;

    protected static void NotifyStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
} 

Thanks!谢谢! I have a feeling I might not be able to get away with this de-duplication attempt, but if it is possible, then that'd be nifty!我有一种感觉,我可能无法摆脱这种重复数据删除尝试,但如果可能的话,那就太好了!

WPF 4.5 supports binding to static properties, all you have to do is create a static event handler of the same name with the text "Changed" appended, and you invoke that instead of your StaticPropertyChanged: WPF 4.5 支持绑定到静态属性,您所要做的就是创建一个同名的静态事件处理程序,并附加文本“Changed”,然后调用它而不是您的 StaticPropertyChanged:

public class MyStaticClass
{
    public static event EventHandler MyStaticPropertyChanged;

    private static string _MyStaticProperty = "Hello World!";
    public static string MyStaticProperty
    {
        get { return _MyStaticProperty; }
        set
        {
            if (_MyStaticProperty != value)
            {
                _MyStaticProperty = value;
                MyStaticPropertyChanged?.Invoke(null, EventArgs.Empty);
            }
        }
    }
}

Which you would bind to like this:你会像这样绑定:

<TextBlock Text="{Binding Path=(vm:MyStaticClass.MyStaticProperty), Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />

Honestly though, this looks like more of an architectural issue to me.老实说,这对我来说更像是一个架构问题。 If you're using a dependency injection framework...and you should be....then there's absolutely no need to use static properties for anything, just place them in their own class and set up your DI to make it a singleton.如果您正在使用依赖注入框架……并且您应该……那么绝对不需要为任何东西使用静态属性,只需将它们放在它们自己的类中并设置您的 DI 使其成为单例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM