gpt4 book ai didi

c# - RelayCommand CanExecute 从不在 Windows 8.1 商店上运行,但在 WPF 上运行

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:52 25 4
gpt4 key购买 nike

我正在使用 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 总是被禁用吗?

最佳答案

您是否必须在命令上调用 RaiseCanExecuteChanged 然后发生一些变化?很长一段时间没有做任何 WPF,但在应用程序中我总是调用 RaiseCanExecuteChanged。

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

关于c# - RelayCommand<PasswordBox> CanExecute 从不在 Windows 8.1 商店上运行,但在 WPF 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26221594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com