RelayCommand<;密码框>;CanExecute从不在Windows8.1存储上运行,但它在WPF上运

本文关键字:运行 存储 上运 WPF Windows8 gt 密码 CanExecute RelayCommand lt | 更新日期: 2023-09-27 18:25:40

我正在用C#和.NET Frameowork 4.5.1开发一个Windows 8.1商店应用程序。

我正试图传递一个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始终处于禁用状态。

我在创建doLoginCommand时删除了CanDoLogin,可以获得_userNameuserPassword.Password的值。

有什么建议吗?你知道为什么loginButton总是被禁用吗?

RelayCommand<;密码框>;CanExecute从不在Windows8.1存储上运行,但它在WPF上运

您不必在命令上调用RaiseCanExecuteChanged,然后发生更改吗?很长时间没有做任何WPF了,但在应用程序中,我总是调用RaiseCanExecuteChanged。

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