Caliburn Micro Guard 方法未评估属性变化

发布于 2024-10-30 04:03:57 字数 656 浏览 5 评论 0原文

我一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2024-11-06 04:03:57

好吧,我想通了。我没有意识到你必须触发守卫方法通知,认为框架做到了这一点,但这是有道理的。

因此,我将属性设置器更改为:

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

现在一切正常:)

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 :)

沫尐诺 2024-11-06 04:03:57

如果您不需要 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.

落在眉间の轻吻 2024-11-06 04:03:57

我假设这些是通过命令调用的(一些关于调用这些方法的代码会有所帮助)。

如果您遇到的情况是您希望命令根据某些输入重新评估,则需要调用 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文