首页 文章

MVVM灯命令没有触发

提问于
浏览
0

我有一个简单的登录表单,我正在努力工作,但由于某种原因,登录按钮不会调用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文本框显示此预定义的字符串,因此我非常确定表单是否正确绑定 . 有什么建议?

1 回答

  • 3

    我认为你的问题是你绑定到公共领域而不是属性 .

    您可以看到可以用作绑定源here on MSDN的东西 . 它不是其中之一 .

    因此,将命令作为属性公开应该可以工作:

    public ICommand LoginCommand { get; private set; }
    

相关问题