Caliburn Micro Guard Methods not evaluating on property change

后端 未结 3 1970
旧时难觅i
旧时难觅i 2021-02-18 23:08

I\'ve been playing with the Caliburn Micro MVVM framework and am having some problems with guard methods.

I have a view model:

public class MyViewModel :         


        
相关标签:
3条回答
  • 2021-02-18 23:28

    I am assuming these are called via a Command (some code around what is call those methods would help).

    If the case you are having is that you want the commands to revevaluate based on some input you need to invoke CommandManager.InvalidateRequerySuggested() so the commands CanExecutes will get called. Since the command is bound to the button and not the textbox it will not update. In your property setter (the one bound to the textbox) you have to tell the framework to requery the commands. This in turn will call your CanCalculate method.

    If the Calculate and CanCalculate methods are not associated with a command then the above will not help.

    0 讨论(0)
  • 2021-02-18 23:33

    Ok I figured it out. I didn't realise that you have to fire the guard method notification, thought the framework did that, but it makes sense.

    So I change my property setter to:

    public DateTime? Date
    {
       get
       {
          return this.date; 
       }
       set
       {
          this.date = value;
          this.NotifyOfPropertyChange(() => Date);
          this.NotifyOfPropertyChange(() => CanCalculate);
       }
    }
    

    and changed my CanCalculate method to a property:

    public bool CanCalculate
    {
        get
        {
            return this.Date.HasValue;
        }
    }
    

    And all works fine now :)

    0 讨论(0)
  • 2021-02-18 23:43

    If you dont need CanExecute to be method, because you wont use parameters. Then you can rewrite it as property with standard notification and only getter. And call its PropertyChanged when you asume result of the getter changed.

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