gpt4 book ai didi

c# - Caliburn Micro Guard Methods 不评估属性变化

转载 作者:IT王子 更新时间:2023-10-29 04:47:22 24 4
gpt4 key购买 nike

我一直在使用 Caliburn Micro MVVM 框架,但在保护方法方面遇到了一些问题。

我有一个 View 模型:

public class MyViewModel : PropertyChangedBase, IMyViewModel

属性:

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

此外,我在我的 View 模型中有一个带有保护方法的方法

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

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

在我看来还有一个按钮:

我遇到的问题是 CanCalculate 方法在加载时执行,但当我在文本字段中输入值时,它不会重新计算 CanCalculate 方法。我在设置数据绑定(bind) View 模型属性时触发了属性更改事件,所以可能是什么问题?

最佳答案

好吧,我明白了。我没有意识到您必须触发保护方法通知,以为框架会这样做,但这是有道理的。

所以我将属性 setter 更改为:

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

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

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

现在一切正常:)

关于c# - Caliburn Micro Guard Methods 不评估属性变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com