A more convenient way to handle property changes in C #

I am creating an MVVM application in which a class ToBeListenedhas a couple of properties, PropertyAand PropertyB, and I want to listen to them.

public class ToBeListened : INotifyPropertyChanged
{

    private double _propertyA;
    private string _propertyB;
    /*Here I'm omitting the update part where NotifyPropertyChanged gets called*/
    public double PropertyA{get; set; }
    public double PropertyB{get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

These two properties are listened to by the class Listener, so I injected an EventHandler into it that listens for the object ToBeListened.

public class Listener
{

     private ToBeListened toBeListenedObject; 

     public Listener()
     {  
        toBeListenedObject = new ToBeListened();
        toBeListenedObject.PropertyChanged += newPropertyChangedEventHandler(PropertyListener_PropertyChanged);
     }

     private void PropertyListener_PropertyChanged(object sender, PropertyChangedEventArgs e)
     {
        switch(e.PropertyName)
        {
          case "PropertyA":
          {
            /*...DO SOMETHING...*/
          }
          case "PropertyB":
          {
            /*...Do something else...*/
          }
    }

The fact is that I do not really like the solution I found. A is switch-casenot compatible with polymorphism, therefore

  • Is there a better way to do this? Maybe something that uses overload ? (Howprivate void PropertyListener_PropertyChanged(double sender, PropertyChangedEventArgs e)
  • First of all, is it correct to encode ViewModel as follows?
+4
source share
3 answers

, Dictionary<string, Action>. :

public class PropertyChangedHandler : Dictionary<string, Action>
{
    public PropertyChangedHandler(INotifyPropertyChanged source)
    {
        source.PropertyChanged += Source_PropertyChanged;
    }

    private void Source_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        Action toDo;
        if (TryGetValue(e.PropertyName, out toDo))
        {
            toDo();
        }
    }
}

:

public class Listener
{
    private ToBeListened toBeListenedObject = new ToBeListened();

    PropertyChangedHandler handler;

    public Listener()
    {
        handler = new PropertyChangedHandler(toBeListenedObject)
                    {
                        { "PropertyA", DoA },
                        { "PropertyB", DoB }
                    };
    }

    private void DoB()
    {            
    }

    private void DoA()
    {            
    }
}

, - , , .

+1

Josh Smith PropertyObserver, http://mvvmfoundation.codeplex.com/ ( https://joshsmithonwpf.wordpress.com/2009/07/11/one-way-to-avoid-messy-propertychanged-event-handling/). , , , . , :

var observer = new PropertyObserver<ToBeListened>(toBeListenedObject)
                  .RegisterHandler(tbl => tbl.PropertyA, tbl => HandlePropertyA(tbl))
                  .RegisterHandler(tbl => tbl.PropertyB, tbl => HandlePropertyB(tbl));

, nvu Foundation MVVM . - MvvmFoundation.Wpf.

+4

I think the MVVM Light framework (or library?) Has what you need. Take a look at the class ObservableObject http://www.mvvmlight.net/help/SL5/html/d457231f-6af7-601d-fa1f-1fe7c9f60c57.htm

Basically, what he does is make your object visible.

0
source

Source: https://habr.com/ru/post/1652334/


All Articles