用WPF MVVM实现一个登录DEMO
时间: 2023-05-13 13:06:15 浏览: 143
好的,以下是一个简单的WPF MVVM登录DEMO的代码示例:
XAML代码:
```
<Window x:Class="LoginDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login Demo" Height="250" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Username:" Margin="10"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Username}" Margin="10"/>
<Label Grid.Row="1" Grid.Column="0" Content="Password:" Margin="10"/>
<PasswordBox Grid.Row="1" Grid.Column="1" Password="{Binding Password}" Margin="10"/>
<Button Grid.Row="2" Grid.Column="1" Content="Login" Command="{Binding LoginCommand}" Margin="10"/>
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding ErrorMessage}" Foreground="Red" Margin="10"/>
<CheckBox Grid.Row="4" Grid.Column="1" Content="Remember me" IsChecked="{Binding RememberMe}" Margin="10"/>
</Grid>
</Window>
```
ViewModel代码:
```
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace LoginDemo
{
public class LoginViewModel : INotifyPropertyChanged
{
private string _username;
private string _password;
private bool _rememberMe;
private string _errorMessage;
public string Username
{
get { return _username; }
set
{
_username = value;
OnPropertyChanged("Username");
}
}
public string Password
{
get { return _password; }
set
{
_password = value;
OnPropertyChanged("Password");
}
}
public bool RememberMe
{
get { return _rememberMe; }
set
{
_rememberMe = value;
OnPropertyChanged("RememberMe");
}
}
public string ErrorMessage
{
get { return _errorMessage; }
set
{
_errorMessage = value;
OnPropertyChanged("ErrorMessage");
}
}
public ICommand LoginCommand { get; set; }
public LoginViewModel()
{
LoginCommand = new RelayCommand(Login);
}
private void Login(object parameter)
{
if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
ErrorMessage = "Please enter both username and password.";
}
else if (Username == "admin" && Password == "password")
{
ErrorMessage = "";
MessageBox.Show("Login successful!");
}
else
{
ErrorMessage = "Invalid username or password.";
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
```
其中,RelayCommand是一个自定义的ICommand实现,用于绑定按钮的Command属性,代码如下:
```
using System;
using System.Windows.Input;
namespace LoginDemo
{
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute?.Invoke(parameter) ?? true;
}
public void Execute(object parameter)
{
_execute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
```
在MainWindow的构造函数中,将ViewModel与View绑定:
```
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new LoginViewModel();
}
}
```
这样,就完成了一个简单的WPF MVVM登录DEMO。
阅读全文