使用参数调用 Caliburn.Micro Can Action 绑定,但不能不使用参数。我对 Caliburn.Micro 很陌生。有人可以解释这种行为吗?我已经浏览了http://caliburnmicro.com/documentation/introduction。
对于以下 Xaml
<TextBox x:Name="YourName" />
<TextBox x:Name="Address" />
<Button x:Name="Save" />
Run Code Online (Sandbox Code Playgroud)
这是有效的:
public void Save(string yourName, string address)
{
MessageBox.Show( $"Your Name : {yourName}{Environment.NewLine}Address : {address}", "You have entered:", MessageBoxButton.OK);
}
public bool CanSave(string yourName, string address) => (!string.IsNullOrWhiteSpace(yourName) && !string.IsNullOrWhiteSpace(address));
Run Code Online (Sandbox Code Playgroud)
这不起作用
public void Save()
{
MessageBox.Show( $"Your Name : {yourName}{Environment.NewLine}Address : {address}", "You have entered:", MessageBoxButton.OK);
}
public bool CanSave() => (!string.IsNullOrWhiteSpace(yourName) && !string.IsNullOrWhiteSpace(address));
Run Code Online (Sandbox Code Playgroud)
TL;DR:只需使用 Can 属性而不是 Can 方法,并PropertyChanged
在前提条件之一可能发生更改时引发事件。
在 Caliburn.Micro 中有两种实现 Action Guards 的方法:
当找到“SayHello”消息的处理程序时,它将检查该类是否也具有名为“CanSayHello”的属性或方法。如果您有一个防护属性并且您的类实现了 INotifyPropertyChanged,那么框架将观察该属性的变化并相应地重新评估防护。
这是你试过的。它适用于具有参数的 Action 方法,因为每次这些参数之一发生变化时,都会重新评估守卫。没有任何魔法在起作用 - 实际上,它只是在幕后使用 WPF 数据绑定将 Action 实例绑定到参数值。一旦绑定引擎检测到更改,就会调用保护方法。这就是为什么在没有参数的情况下它不会工作的原因,并且没有办法自己轻松触发守卫。
这就是你应该在你的情况下使用的:添加一个 Get-only 属性,就像这样:
public bool CanSave => (!string.IsNullOrWhiteSpace(yourName)
&& !string.IsNullOrWhiteSpace(address));
Run Code Online (Sandbox Code Playgroud)
然后,无论何时yourname
或address
更改,只需调用:
this.NotifyOfPropertyChange(nameof(CanSave));
Run Code Online (Sandbox Code Playgroud)
这样,框架就会知道 Guard 的值可能已经改变并重新评估它。
归档时间: |
|
查看次数: |
1425 次 |
最近记录: |