wpf mvvm 登录界面代码
时间: 2023-05-08 16:59:23 浏览: 120
登录界面代码
WPF MVVM模式的登录界面代码可以分为视图(View)、模型(Model)和视图模型(ViewModel)三部分。
首先是视图(View)部分,该部分主要负责呈现应用程序用户界面,以及与用户之间的交互。下面是一个简单的登录视图的XAML界面代码:
```xaml
<Window x:Class="WpfApplication1.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LoginView" Height="200" Width="350">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="用户名:" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Username, UpdateSourceTrigger=PropertyChanged}" Width="150" Margin="5"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="密码:" VerticalAlignment="Center"/>
<PasswordBox Grid.Row="1" Grid.Column="1" Password="{Binding Password, UpdateSourceTrigger=PropertyChanged}" Width="150" Margin="5"/>
<CheckBox Grid.Row="2" Grid.Column="1" Content="记住密码" VerticalAlignment="Center" IsChecked="{Binding RememberMe, Mode=TwoWay}" Margin="5"/>
<Button Grid.Row="3" Grid.ColumnSpan="2" Content="登录" Command="{Binding LoginCommand}" Width="100" Margin="5" HorizontalAlignment="Right"/>
<TextBlock Grid.Row="4" Grid.ColumnSpan="2" Text="{Binding ErrorMessage}" Foreground="Red" Margin="5"/>
</Grid>
</Window>
```
接着是模型(Model)部分,该部分主要定义应用程序的数据模型以及相关操作,例如存储用户名密码等信息,以及验证登录信息是否正确。下面是一个简单的登录模型的C#代码:
```csharp
public class LoginModel
{
private readonly IDictionary<string, string> _users = new Dictionary<string, string>
{
{"user1", "password1"},
{"user2", "password2"},
{"user3", "password3"}
};
public bool ValidateUser(string username, string password)
{
if (_users.ContainsKey(username) && _users[username] == password)
{
return true;
}
return false;
}
}
```
最后是视图模型(ViewModel)部分,该部分主要完成视图(View)与模型(Model)之间的数据绑定和交互。下面是一个简单的登录视图模型的C#代码:
```csharp
public class LoginViewModel : INotifyPropertyChanged
{
private readonly LoginModel _model = new LoginModel();
private string _username;
public string Username
{
get { return _username; }
set
{
if (_username != value)
{
_username = value;
RaisePropertyChanged("Username");
}
}
}
private string _password;
public string Password
{
get { return _password; }
set
{
if (_password != value)
{
_password = value;
RaisePropertyChanged("Password");
}
}
}
private bool _rememberMe;
public bool RememberMe
{
get { return _rememberMe; }
set
{
if (_rememberMe != value)
{
_rememberMe = value;
RaisePropertyChanged("RememberMe");
}
}
}
private string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set
{
if (_errorMessage != value)
{
_errorMessage = value;
RaisePropertyChanged("ErrorMessage");
}
}
}
private RelayCommand _loginCommand;
public RelayCommand LoginCommand
{
get
{
if (_loginCommand == null)
{
_loginCommand = new RelayCommand(
() =>
{
if (_model.ValidateUser(_username, _password))
{
ErrorMessage = "";
MessageBox.Show("登录成功!");
}
else
{
ErrorMessage = "用户名或密码错误!";
}
},
() =>
{
return !string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password);
}
);
}
return _loginCommand;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
```
以上就是WPF MVVM模式的简单登录界面代码。
阅读全文