Caliburn 命令评估仅触发一次
Caliburn command evaluation only fired once
我正在尝试使用 Caliburns 'Can' 视图模型 属性 评估约定在我的视图上启用按钮。
查看(节选)
<PasswordBox PasswordChanged="PasswordBox_OnPasswordChanged" Grid.Row="1" Grid.Column="1" />
...
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Cancel" cal:Message.Attach="[Event Click] = [Action Cancel]" />
<Button Content="Login" cal:Message.Attach="[Event Click] = [Action Login]" />
</StackPanel>
代码隐藏
private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
if (DataContext != null)
((dynamic) DataContext).Password = ((PasswordBox) sender).Password;
}
视图模型
public class LoginSplashViewModel : Screen
{
private string _username;
private string _password;
public string Username
{
get { return _username; }
set
{
_username = value;
NotifyOfPropertyChange();
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
NotifyOfPropertyChange();
}
}
public LoginSplashViewModel()
{
DisplayName = "Login";
}
public bool CanLogin()
{
return !string.IsNullOrEmpty(_username) ||
!string.IsNullOrEmpty(_password);
}
public void Login()
{
TryClose(true);
}
public void Cancel()
{
TryClose(false);
}
}
然而,'CanLogin()'
方法只触发一次(当将视图模型绑定到视图时),并且再也不会触发,因此按钮保持禁用状态。
我是不是漏掉了什么?
这就是Caliburn中命令的实现方式。基本上有个东西可以调用到force reevaluation of CanExecute methods in ICommand implementors.
每当有可以在代码隐藏或 viewModel 中识别的更改时。此外,您可以选择自己实施命令并避免这种必要性。
public string Password{
get{ return _password;}
set{
_password = value;
NotifyOfPropertyChange();
NotifyOfPropertyChange(() => CanLogin); // <--- Addition
}
}
用户名也一样...
对于它的价值,您也不必对事件的长形式...
你可以做
<Button x:Name="Login" />
我正在尝试使用 Caliburns 'Can' 视图模型 属性 评估约定在我的视图上启用按钮。
查看(节选)
<PasswordBox PasswordChanged="PasswordBox_OnPasswordChanged" Grid.Row="1" Grid.Column="1" />
...
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Cancel" cal:Message.Attach="[Event Click] = [Action Cancel]" />
<Button Content="Login" cal:Message.Attach="[Event Click] = [Action Login]" />
</StackPanel>
代码隐藏
private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
if (DataContext != null)
((dynamic) DataContext).Password = ((PasswordBox) sender).Password;
}
视图模型
public class LoginSplashViewModel : Screen
{
private string _username;
private string _password;
public string Username
{
get { return _username; }
set
{
_username = value;
NotifyOfPropertyChange();
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
NotifyOfPropertyChange();
}
}
public LoginSplashViewModel()
{
DisplayName = "Login";
}
public bool CanLogin()
{
return !string.IsNullOrEmpty(_username) ||
!string.IsNullOrEmpty(_password);
}
public void Login()
{
TryClose(true);
}
public void Cancel()
{
TryClose(false);
}
}
然而,'CanLogin()'
方法只触发一次(当将视图模型绑定到视图时),并且再也不会触发,因此按钮保持禁用状态。
我是不是漏掉了什么?
这就是Caliburn中命令的实现方式。基本上有个东西可以调用到force reevaluation of CanExecute methods in ICommand implementors.
每当有可以在代码隐藏或 viewModel 中识别的更改时。此外,您可以选择自己实施命令并避免这种必要性。
public string Password{
get{ return _password;}
set{
_password = value;
NotifyOfPropertyChange();
NotifyOfPropertyChange(() => CanLogin); // <--- Addition
}
}
用户名也一样...
对于它的价值,您也不必对事件的长形式...
你可以做
<Button x:Name="Login" />