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
,我可以得到_userName
和userPassword.Password
值。
有什麼建議嗎?你知道爲什麼loginButton
始終被禁用嗎?
如果您將任何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
是的,我知道我在做什麼。我在問爲什麼這個代碼在Windows 8.1 Store上不起作用。 – VansFannel 2014-10-06 18:05:35
*不起作用*對您的問題不是很好的描述......並非如此。 – Sheridan 2014-10-06 18:08:42