M
M
Maxim Nikitin2015-11-18 16:35:41
.NET
Maxim Nikitin, 2015-11-18 16:35:41

How to override subscription to PropertyChanged?

There is a class that implements INotifyPropertyChanged, hence there is an event in the class

public event PropertyChangedEventHandler PropertyChanged

This event is subscribed to in more than 300 places with a simple += operator. There is a goal to switch to using WeakEventManager to avoid memory leaks. Of course, I don’t want to change the subscription method at all in 300 places, so I want to get out somehow.
All I could think of (but it doesn't work):
public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                WeakEventManager<BaseNotifyPropertyChanged, EventArgs>.AddHandler(this, "PropertyChanged", (EventHandler<EventArgs>)value);
//либо же
                PropertyChangedEventManager.AddHandler(this, value, "IsDirty");
            }
            remove
            {
               //аналогичный код
            }
        }

In both the WeakEventManager and PropertyChangedEventManager cases, the compiler complains about the impossibility of casting a PropertyChangedEventHandler to an EventHandler. Is it possible to somehow solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rasim Keita, 2015-11-20
@keyros

try to do like this,
I didn’t understand much about what needs to be done, but I believed you and that’s it.

public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                WeakEventManager<INotifyPropertyChanged, PropertyChangedEventArgs>.AddHandler(this, "PropertyChanged", new EventHandler<PropertyChangedEventArgs>(value));
                //либо же
                PropertyChangedEventManager.AddHandler(this, new EventHandler<PropertyChangedEventArgs>(value), "IsDirty");
            }
            remove
            {
                WeakEventManager<INotifyPropertyChanged, PropertyChangedEventArgs>.RemoveHandler(this, "PropertyChanged", new EventHandler<PropertyChangedEventArgs>(value));

               
                //аналогичный код
                PropertyChangedEventManager.RemoveHandler(this, new EventHandler<PropertyChangedEventArgs>(value), "IsDirty");
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question