A cleaner way to automatically call one method after another?

前端 未结 7 1356
北荒
北荒 2021-01-18 02:28

Is it possible to design a method in such a fashion, that it knows it must automatically call a next method in succession upon exiting?

In the following example, I m

相关标签:
7条回答
  • 2021-01-18 03:17

    Taking into consideration your particular problem and the solutions posted, I would say the "cleanest" approach here would be to implement a Property Changed Notification just for internal use in the form i.e. you don't need to expose the event like in the MSDN example.

    This way you could maintain an internal list of properties that you know will require the form to be refreshed e.g.

      private List<string> _refreshProps = new List<string>();
      private bool _showPriority;
    
      public void Form()
      {
          _refreshProps.Add("ShowPriority");
          ... etc
      }
    
      // only implement properties like this that need some extra work done
      public bool ShowPriority
      {
          get { return _showPriority; }
          set
          {
              if (_showPriority != value)
              {
                  _showPriority = value;
                  // Call OnPropertyChanged whenever the property is updated
                  OnPropertyChanged("ShowPriority");
              }
          }
      }
    
      // basic property that doesn't require anything extra
      public bool AnotherProperty { get; set; }
    
      public void Refresh()
      {
          // refresh the form
      }
    
      protected void OnPropertyChanged(string name)
      {
          if (_refreshProps.Contains(name))
              Refresh();
      }
    

    The benefit of this approach is if in the future you needed to do other "stuff" after particular properties you can simply introduce another list and handle it again in your OnPropertyChanged method.

    0 讨论(0)
提交回复
热议问题