RelayCommand< PasswordBox> CanExecute永远运行在Windows 8.1存储,但它确实对WPF [英] RelayCommand<PasswordBox> CanExecute never runs on Windows 8.1 store but it does on WPF

查看:232
本文介绍了RelayCommand< PasswordBox> CanExecute永远运行在Windows 8.1存储,但它确实对WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a Windows 8.1 store app with C# and .NET Frameowork 4.5.1.

我想传递一个 PasswordBox 作为一个按钮命令的参数。我测试过的WPF下面的代码和它的作品

I'm trying to pass a PasswordBox as a parameter on a button Command. I've tested the following code on WPF and it works.

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)));
    }
}

和它的查看

<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 创作,我可以得到 _username userPassword.Password 值。

I have removed CanDoLogin on doLoginCommand creation and I can get _userName and userPassword.Password values.

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

Any advice? Do you know why loginButton is always disabled?

推荐答案

鸵鸟政策你必须调用RaiseCanExecuteChanged的命令,然后什么变化? Haven't做过任何WPF在很长一段时间,但在我的应用程序始终调用RaiseCanExecuteChanged。

Don´t you have to call RaiseCanExecuteChanged on the command then something changes? Haven´t done any WPF in a long time but in apps I always call RaiseCanExecuteChanged.

时,这种情况下我会打电话RaiseCanExecuteChanged对PasswordChanged事件。

Is this case I would call RaiseCanExecuteChanged on the PasswordChanged event.

这篇关于RelayCommand&LT; PasswordBox&GT; CanExecute永远运行在Windows 8.1存储,但它确实对WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆