2014-10-06 53 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。

相關問題