在继承的类中复制EventHandler

时间:2016-08-12 10:10:49

标签: c# wpf inotifypropertychanged

我正在使用c#WPF开发一个应用程序。

我使用课程PropertyChangedNotifier来管理INotifyPropertyChanged(请参阅that link)。

我使用经典的ViewModelBase:

public class ViewModelBase : INotifyPropertyChanged, IRequestClose
{
    public PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = _propertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

在我的MainViewModel : ViewModelBase我有PropertyChangedNotifier<MainViewModel>这样的工作:

class MainViewModel : ViewModelBase
{
    private PropertyChangedNotifier<MainViewModel> _notifier;
    public new event PropertyChangedEventHandler PropertyChanged
    {
        add { _notifier.PropertyChanged += value; }
        remove { _notifier.PropertyChanged -= value; }
    }

    public MainViewModel()
    {
        _notifier = new PropertyChangedNotifier<MainViewModel>(this);
    }

    protected new void OnPropertyChanged(string propertyName)
    {
        _notifier.OnPropertyChanged(propertyName);
    }
}

但是未检测到更改,当值更改时,我的MainWindows不刷新(不使用PropertyChangedNotifier,它可以工作)。我看到系统使用WindowsBase.dll!System.ComponentModel.PropertyChangedEventManager.StartListening(object source)启动窗口,然后我看到当调用MainViewModel的构造函数时,我的ViewModelBase.PropertyChanged不为null。

是否可以制作这样的东西:

public MainViewModel()
{
    _notifier = new PropertyChangedNotifier<MainViewModel>(this);
    _notifier.PropertyChanged = base.PropertyChanged;
}

那会修复我的错误吗?

编辑:

来自链接的

PropertyChangeNotifier:

[AttributeUsage( AttributeTargets.Property )]
public class DepondsOnAttribute : Attribute
{
    public DepondsOnAttribute( string name )
    {
        Name = name;
    }

    public string Name { get; }
}

public class PropertyChangedNotifier<T> : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public PropertyChangedNotifier( T owner )
    {
        mOwner = owner;
    }

    public void OnPropertyChanged( string propertyName )
    {
        var handler = PropertyChanged;
        if( handler != null ) handler( mOwner, new PropertyChangedEventArgs( propertyName ) );

        List<string> dependents;
        if( smPropertyDependencies.TryGetValue( propertyName, out dependents ) )
        {
            foreach( var dependent in dependents ) OnPropertyChanged( dependent );
        }
    }

    static PropertyChangedNotifier()
    {
        foreach( var property in typeof( T ).GetProperties() )
        {
            var dependsOn = property.GetCustomAttributes( true )
                                    .OfType<DepondsOnAttribute>()
                                    .Select( attribute => attribute.Name );

            foreach( var dependency in dependsOn )
            {
                List<string> list;
                if( !smPropertyDependencies.TryGetValue( dependency, out list ) )
                {
                    list = new List<string>();
                    smPropertyDependencies.Add( dependency, list );
                }

                if (property.Name == dependency)
                    throw new ApplicationException(String.Format("Property {0} of {1} cannot depends of itself", dependency, typeof(T).ToString()));

                list.Add( property.Name );
            }
        }
    }

    private static readonly Dictionary<string, List<string>> smPropertyDependencies = new Dictionary<string, List<string>>();

    private readonly T mOwner;
}

2 个答案:

答案 0 :(得分:0)

问题是,当我创建MainViewModel时,base.PropertyChanged为空。调用构造函数,初始化fields / properties(一些调用OnPropertyChanged),然后在MainView.InitializeComponent()之后在base.PropertyChanged中添加一个处理程序(我附加了一个调试器来查看)。此添加在ViewModelBase中完成,忽略了public new event PropertyChangedEventHandler PropertyChanged中的MainViewModel

我想检查base.PropertyChanged是否已被修改并复制。

我已经做到了这一点,我不知道是否有一个好主意,你怎么看?

public class ViewModelBase : INotifyPropertyChanged, IRequestClose
{
    protected event PropertyChangedEventHandler _propertyChanged;
    protected bool propertyAdded { get; private set; }
    public event PropertyChangedEventHandler PropertyChanged
    {
        add
        {
            _propertyChanged += value;
            propertyAdded = true;
        }
        remove { _propertyChanged -= value; }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = _propertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public Delegate[] Get_PropertyChanged()
    {
        isPropertyAdded = false;
        Delegate[] delegates = new Delegate[0];
        if (_propertyChanged != null)
        {
            delegates = _propertyChanged.GetInvocationList();
            _propertyChanged = null;
        }

        return delegates;
    }
}

class MainViewModel : ViewModelBase
{
    private PropertyChangedNotifier<MainViewModel> _notifier;
    public new event PropertyChangedEventHandler PropertyChanged
    {
        add { _notifier.PropertyChanged += value; }
        remove { _notifier.PropertyChanged -= value; }
    }

    public MainViewModel()
    {
        _notifier = new PropertyChangedNotifier<MainViewModel>(this);
    }

    protected new void OnPropertyChanged(string propertyName)
    {
        if (isPropertyAdded)
        {
            foreach (Delegate d in Get_PropertyChanged())
            {
                _notifier.PropertyChanged += (PropertyChangedEventHandler)d;
            }
        }

        _notifier.OnPropertyChanged(propertyName);
    }
}

答案 1 :(得分:0)

对于简单的NotifyPropertyChanged("dependentProperty")来说,似乎是一个复杂的解决方案,尽管我确实理解设计奇特解决方案的愿望。如果它有一些性能/本地优势会有意义,但事实并非如此。它只会增加复杂性。