Caliburn Micro Guard 方法未评估属性变化
我一直在使用 Caliburn Micro MVVM 框架,但在防护方法方面遇到了一些问题。
我有一个视图模型:
public class MyViewModel : PropertyChangedBase, IMyViewModel
一个属性:
public DateTime? Date
{
get{return this.date; }
set
{
this.date = value;
this.NotifyOfPropertyChange(() => Date);
}
}
另外,我的视图模型中有一个带有保护方法的方法
public void Calculate()
{
// ..some code..
}
public bool CanCalculate()
{
return this.Date.HasValue;
}
,并且在我的视图中有一个按钮:
我遇到的问题是 CanCalculate 方法在加载时执行,但当我在文本字段中输入值时,它不会重新评估 CanCalculate 方法。我在设置数据绑定视图模型属性时触发属性更改事件,那么可能是什么问题?
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 : PropertyChangedBase, IMyViewModel
A property:
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想通了。我没有意识到你必须触发守卫方法通知,认为框架做到了这一点,但这是有道理的。
因此,我将属性设置器更改为:
并将我的
CanCalculate
方法更改为属性:现在一切正常:)
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:
and changed my
CanCalculate
method to a property:And all works fine now :)
如果您不需要 CanExecute 作为方法,因为您不会使用参数。然后您可以将其重写为具有标准通知和仅 getter 的属性。当你认为 getter 的结果发生改变时,调用它的 PropertyChanged。
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.
我假设这些是通过命令调用的(一些关于调用这些方法的代码会有所帮助)。
如果您遇到的情况是您希望命令根据某些输入重新评估,则需要调用 CommandManager.InvalidateRequerySuggested() 以便调用命令 CanExecute。由于该命令绑定到按钮而不是文本框,因此它不会更新。在属性设置器(绑定到文本框的属性设置器)中,您必须告诉框架重新查询命令。这将依次调用您的
CanCalculate
方法。如果Calculate 和CanCalculate 方法未与命令关联,则上述方法将无济于事。
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 commandsCanExecute
s 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 yourCanCalculate
method.If the Calculate and CanCalculate methods are not associated with a command then the above will not help.