Caliburn Micro Guard方法未评估属性更改 [英] Caliburn Micro Guard Methods not evaluating on property change

查看:54
本文介绍了Caliburn Micro Guard方法未评估属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Caliburn Micro MVVM框架,并且在保护方法方面遇到一些问题.

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

我有一个视图模型:

public class MyViewModel : PropertyChangedBase, IMyViewModel

财产:

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

此外,我的视图模型中有一个带有防护方法的方法

Also, i have a method in my view model with a guard method

public void Calculate()
{
    // ..some code..
}

public bool CanCalculate()
{
    return this.Date.HasValue;
}

在我看来还有一个按钮:

And a button in my view:

我遇到的问题是,加载时会执行CanCalculate方法,但是当我在文本字段中输入值时,它不会重新评估CanCalculate方法.我在设置数据绑定视图模型属性时触发了属性更改事件,那么可能是什么问题?

The problem I am having is that the CanCalculate method executes when loading but when I enter values into the text fields, it doesn't reevaluate the CanCalculate method. I am firing the property changed event on setting the databound view model properties so what could be the problem?

推荐答案

好,我知道了.我没有意识到您必须解雇警卫方法通知,以为框架已经做到了,但这是有道理的.

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);
   }
}

并将我的 CanCalculate 方法更改为属性:

and changed my CanCalculate method to a property:

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

现在一切正常:)

这篇关于Caliburn Micro Guard方法未评估属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆