我正在使用 ReactiveCommand:
LoginCommand = new ReactiveCommand(this.WhenAny(x => x.UserName, x => x.Password, (r, g) => !string.IsNullOrEmpty(r.Value) &&!string.IsNullOrEmpty(g.Value)));
LoginCommand.Subscribe(async _ =>
{
var user = await _authenticationService.AuthenticateUser(_userName, _password);
if (user.Error != null)
{
MessageBox.Show("The user was not found. Please try again!", "User Not Found", MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
screen.Router.Navigate.Execute(new MainViewModel(screen));
});
以及用于绑定(bind)的用户名、密码字段:
public string Password
{
get { return _password; }
set { this.RaiseAndSetIfChanged(ref _password, value); }
}
并将此绑定(bind)的文本框用于:
<TextBox x:Name="txtPassword" Text="{Binding Password}" Grid.Column="1" Grid.Row="2" Margin="3,3,0,3" MinWidth="100" HorizontalAlignment="Left" Width="10" Height="30"/>
此代码有效,但是当我失去对文本框的关注时。当我开始使用响应式 UI 在文本框上写作时,有什么合适的方法可以做到这一点。
最佳答案
使用 UpdateSourceTrigger 属性。顾名思义,这意味着只要 TextBox 的 Text 属性发生变化,就会触发 RaiseAndSetIfChanged。
<TextBox x:Name="txtPassword" Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="2" Margin="3,3,0,3" MinWidth="100" HorizontalAlignment="Left" Width="10" Height="30"/>
关于mvvm - ReactiveUI RaiseAndSetIfChanged 在文本框失去焦点时命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24606512/