【发布时间】:2014-08-03 20:06:42
【问题描述】:
我有一个简单的登录表单,我正在尝试使用它,但由于某种原因,登录按钮没有调用 LoginCommand,我不知道为什么,有人可以帮我...
这是表单的代码:
<Window x:Class="App.Views.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login" Height="200" Width="340"
DataContext="{Binding Login, Source={StaticResource Locator}}" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="Username:" HorizontalAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Padding="0,1,5,0"/>
<Label Content="Password:" HorizontalAlignment="Right" VerticalAlignment="Top" Padding="0,1,5,0" Grid.Row="3" Grid.Column="1"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="20" Grid.Row="1" Grid.Column="2" Text="{Binding UserName}"/>
<PasswordBox x:Name="password" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="20" Grid.Row="3" Grid.Column="2"/>
<Button Command="{Binding Source=LoginCommand}" CommandParameter="{Binding ElementName=password}" Content="Login" Grid.Row="5" Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Center" Width="60"/>
</Grid>
</Window>
这是我的 LoginViewModel:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Net.Http;
using System.Windows.Controls;
using System.Windows.Input;
namespace App.ViewModels
{
public class LoginViewModel : ViewModelBase
{
public Login LoginModel;
public ICommand LoginCommand;
public ICommand RegisterViewCommand;
public ICommand ExitViewCommand;
public LoginViewModel()
{
LoginModel = new Models.Login();
LoginCommand = new RelayCommand<object>(p => Login(p));
RegisterViewCommand = new RelayCommand(() => Register());
ExitViewCommand = ApplicationCommands.Close;
}
public string UserName
{
get
{
return LoginModel.UserName;
}
set
{
if(value != LoginModel.UserName)
{
LoginModel.UserName = value;
RaisePropertyChanged("UserName");
}
}
}
#region Private Helpers
private void Register()
{
throw new NotImplementedException();
}
private async void Login(object parameter)
{
var passwordBox = parameter as PasswordBox;
LoginModel.Password = passwordBox.Password;
LoginModel.RememberMe = true;
var response = await HttpClientSingleton.Instance.PostAsJsonAsync("http://localhost:52841/Account/ClientLogin", LoginModel);
bool success;
if (bool.TryParse(await response.Content.ReadAsStringAsync(), out success))
{
}
}
#endregion Private Helpers
}
}
我在 Login 方法的第一行设置了一个断点,但它从未真正命中它。我还更改了 UserName 属性以返回一个预定义的字符串来测试 DataContext 绑定,并且 UserName 文本框显示这个预定义的字符串,所以我很确定表单被正确绑定。有什么建议吗?
【问题讨论】:
标签: wpf mvvm-light