为了熟悉WinVM的MVVM,我一直在研究MvvmLight WinRT项目的示例。目前我遇到的问题是我的RelayCommand只被调用一次(在构建viewmodel时)。我想要做的是如果用户被授权,请转到MainViewModel。如果我在LoginCommand方法中删除登录的条件检查,则RelayCommand按预期工作。关于我做错了什么的任何想法?我应该不在LoginCommand中进行验证吗?
LoginViewModel(某些代码已被删除):
public class LoginViewModel : ViewModelBase {
private readonly IDataService _dataService;
private readonly INavigationService _navigationService;
private RelayCommand _navigateCommand;
private Login login; //contains username and password
/// <summary>
/// Gets the NavigateCommand.
/// THIS DOES NOT GET FIRED UPON BUTTON CLICK
/// </summary>
public RelayCommand LoginCommand{
get {
if (login != null && login.UserName.Equals("Test"))
return _navigateCommand ?? (_navigateCommand = new RelayCommand(() => _navigationService.Navigate(typeof(MainPage))));
return _navigateCommand;
}
}
LoginPage.xaml.cs(某些代码已被删除):
public sealed partial class LoginPage {
public LoginViewModel Vm {
get { return (LoginViewModel)DataContext; }
}
public LoginPage() {
InitializeComponent();
}
protected override void LoadState(object state) {
var casted = state as LoginPageState;
if (casted != null) {
Vm.Load(casted);
}
}
protected override object SaveState() {
return new LoginPageState {
Credentials = new Login {
UserName = txtUserName.Text,
Password = txtPassword.Text
}
};
}
public class LoginPageState {
public Login Credentials { get; set; }
}
}
}
LoginPage.xaml(某些代码已被删除)
<Button Content="Login"
x:Name="NextPageButton"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Margin="10"
Command="{Binding LoginCommand}" />
答案 0 :(得分:1)
问题在于条件。加载LoginPage
后,它会尝试将您的Button
绑定到LoginCommand
。为了实现这一目标,它会从LoginCommand
获得ViewModel
。在那个时间点Login
为空,因此该属性将返回_navigateCommand
null
。之后页面将不会尝试使用LoginCommand
,因为它已经知道它的值。
要解决此问题,您可以在lambda表达式中移动条件:
public RelayCommand LoginCommand
{
get
{
return _navigateCommand ?? (_navigateCommand = new RelayCommand(
() =>
{
if (login != null && login.UserName.Equals("Test"))
{
_navigationService.Navigate(typeof(MainPage));
}
}));
}
}
更好的解决方案是将授权转移到另一个类。类似的东西:
public RelayCommand LoginCommand
{
get
{
return _navigateCommand ?? (_navigateCommand = new RelayCommand(
() =>
{
if (_authorizationService.UserAuthorized(login))
{
_navigationService.Navigate(typeof(MainPage));
}
}));
}
}
希望这有帮助。