2014-10-06 38 views
2

我正在开发一个Windows 8.1商店应用程序,使用C#和.NET Frameowork 4.5.1。RelayCommand <PasswordBox> CanExecute从未在Windows 8.1商店运行,但它在WPF上运行

我试图通过PasswordBox作为按钮Command上的参数。我已经测试了WPF上的以下代码,它的工作原理。

MainViewModel类:

public class MainViewModel : ViewModelBase 
{ 
    private RelayCommand<PasswordBox> doLoginCommand; 

    /// <summary> 
    /// The <see cref="UserName" /> property's name. 
    /// </summary> 
    public const string UserNamePropertyName = "UserName"; 

    private string _userName = string.Empty; 

    /// <summary> 
    /// Sets and gets the UserName property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public string UserName 
    { 
     get 
     { 
      return _userName; 
     } 
     set 
     { 
      Set(UserNamePropertyName, ref _userName, value); 
     } 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    public RelayCommand<PasswordBox> DoLoginCommand 
    { 
     get { return doLoginCommand; } 
    } 

    /// <summary> 
    /// Initializes a new instance of the MainViewModel class. 
    /// </summary> 
    public MainViewModel() 
    { 
     ////if (IsInDesignMode) 
     ////{ 
     //// // Code runs in Blend --> create design time data. 
     ////} 
     ////else 
     ////{ 
     //// // Code runs "for real" 
     ////} 

     this.doLoginCommand = new RelayCommand<PasswordBox>((pb) => ExecuteDoLogin(pb), (pb) => CanDoLogin(pb)); 
     //this.doLoginCommand = new RelayCommand<PasswordBox>((pb) => ExecuteDoLogin(pb)); 
    } 

    private void ExecuteDoLogin(object parameter) 
    { 
     PasswordBox passwordBox = parameter as PasswordBox; 
     Debug.WriteLine(_userName); 
     Debug.WriteLine(passwordBox.Password); 
    } 

    private bool CanDoLogin(object parameter) 
    { 
     Debug.WriteLine("CanDoLogin"); 
     PasswordBox passwordBox = parameter as PasswordBox; 
     return ((!string.IsNullOrEmpty(_userName)) && 
      (!string.IsNullOrEmpty(passwordBox.Password))); 
    } 
} 

而其View

<Page 
    x:Class="MyProject.W81.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyProject.W81" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    DataContext="{Binding MainViewModel, Source={StaticResource Locator}}" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="33*"/> 
      <ColumnDefinition Width="77*"/> 
     </Grid.ColumnDefinitions> 
     <StackPanel Grid.Column="1" HorizontalAlignment="Center" Height="400" Margin="0" VerticalAlignment="Center" Width="600"> 
      <TextBox 
       x:Name="userName" 
       TextWrapping="Wrap" 
       HorizontalAlignment="Left" 
       VerticalAlignment="Top" 
       Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       Width="247" 
       Margin="10,10,10,5"/> 
      <PasswordBox 
       x:Name="userPassword" 
       HorizontalAlignment="Left" 
       VerticalAlignment="Top" 
       Width="250" 
       FontFamily="Global User Interface" 
       Margin="10,5"/> 
      <Button 
       x:Name="loginButton" 
       Content="Login" 
       HorizontalAlignment="Left" 
       VerticalAlignment="Stretch" 
       Command="{Binding DoLoginCommand}" 
       CommandParameter="{Binding ElementName=userPassword}" 
       Margin="10,5" /> 
     </StackPanel> 
    </Grid> 
</Page> 

但在Windows 8.1中这是行不通的。问题是loginButton始终处于禁用状态。

我已经删除CanDoLogin创建doLoginCommand,我可以得到_userNameuserPassword.Password值。

有什么建议吗?你知道为什么loginButton始终被禁用吗?

+0

如果您将任何UI元素作为'ICommand'参数传递,您就不会*使用MVVM ...视图模型不应该知道关于UI的任何信息。你也许应该看看[如何绑定到MVVM中的PasswordBox](http://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm)问题。 – Sheridan 2014-10-06 17:54:36

+0

是的,我知道我在做什么。我在问为什么这个代码在Windows 8.1 Store上不起作用。 – VansFannel 2014-10-06 18:05:35

+0

*不起作用*对您的问题不是很好的描述......并非如此。 – Sheridan 2014-10-06 18:08:42

回答

1

您不必在命令中调用RaiseCanExecuteChanged,那么一些更改?很长一段时间没有做任何WPF,但在应用程序中我总是调用RaiseCanExecuteChanged。

这种情况下,我会在PasswordChanged事件上调用RaiseCanExecuteChanged。

相关问题