MVVM update of calculated properties

前端 未结 5 664
粉色の甜心
粉色の甜心 2020-12-17 21:54

I\'m just learning MVVM, and I\'m trying to work how to display changes to a calculated property as a result of changes to the values from which it\'s calculated. All of the

5条回答
  •  甜味超标
    2020-12-17 22:23

    Check out Stephen Cleary's Calculated Properties: https://github.com/StephenCleary/CalculatedProperties

    It's very simple and does just this: propagates notifications of dependant properties without polluting the trigger property setter.

    Primitive example:

    public string Name 
    {
      get { return Property.Get(string.Empty); }
      set { Property.Set(value); }
    } 
    
    public string Greeting => Property.Calculated(() => "Hello, " + Name + "!");
    

    It is incredibly powerful for its size: think Excel-like formula engine for View Model properties.

    I used it in several projects both in domain and view model classes, it helped me to eliminate most of imperative control flow (a major source of errors) and make code much more declarative and clear.

    The best thing about it is that dependent properties can belong to different view models and dependency graph can change dramatically during runtime and it still just works.

提交回复
热议问题