WPF(Windows Presentation Foundation) Windows 桌面应用程序的技术框架,而 MVVM(Model-View-ViewModel)WPF 应用程序中组织和管理代码的架构模式。
用户界面(View)与业务逻辑(Model)分离,并引入一个中介层(ViewModel)
页面部分 :TextBox直接用Binding绑定即可使用 Button则需要引入Command进行绑定
<TextBlock Text="用户名" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0" />
<TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="1" Margin="5" />
<TextBlock Text="密码" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" />
<TextBox Text="{Binding Password}" Grid.Row="1" Grid.Column="1" Margin="5" />
<Button x:Name="Login_Btn" Grid.Row="3" Grid.ColumnSpan="2" Content="登录" Command="{Binding LoginAction }" />
后台部分:只需要引入然后把引入的参数及其方法指向当前的DataContext
public partial class MainWindow : Window
{
LoginVM loginVm; // LoginVM 类的实例,用于处理与登录界面相关的逻辑和数据
public MainWindow()
{
InitializeComponent();
loginVm = new LoginVM(); // LoginVM 类的实例,用于处理与登录界面相关的逻辑和数据
this.DataContext = loginVm; // 将主窗口的 DataContext 设置为 loginVm,用于绑定界面元素与 loginVm 的属性和命令
}
}
方法及数据处理类:信息交流中心 ,参数方法都在此操作
public class LoginVM : INotifyPropertyChanged
// 实现 INotifyPropertyChanged 接口的类,用于在属性值改变时通知界面更新
{
public LoginVM()
{
// 在构造函数中初始化 LoginAction 和 RegisterAction 命令
LoginAction = new RelayCommand(LoginFunc, CanLogionRxecute);
RegisterAction = new RelayCommand(RegisterFunc, CanLogionRxecute);
}
public event PropertyChangedEventHandler PropertyChanged;// PropertyChanged 事件,当属性值发生改变时会触发该事件
private void ReadChange(string propertyName)// 触发 PropertyChanged 事件的方法
// 参数 propertyName:发生改变的属性名
{
PropertyChangedEventHandler hander = PropertyChanged;// 获取 PropertyChanged 事件的委托列表
if (hander != null) hander(this, new PropertyChangedEventArgs(propertyName));// 获取 PropertyChanged 事件的委托列表
}
private LoginModel LoginPar = new LoginModel();
public string UserName
{
get { return LoginPar.UserName; }
set
{
LoginPar.UserName = value;
ReadChange("UserName");
}
}
public string Password
{
get { return LoginPar.Password; }
set
{
LoginPar.Password = value;
ReadChange("Password");
}
}
void RegisterFunc() {
//注册逻辑
}
void LoginFunc()
{
if (UserName == "admin" && Password == "88888888")
{
index index = new index();
index.Show();
}
else
{
MessageBox.Show($"账号密码错误!!!");
UserName = "";
Password = "";
}
}
bool CanLogionRxecute()
{
return true; // 实现注册的 CanExecute 逻辑
}
public ICommand LoginAction { get; }// 命令 绑定到注册按钮
public ICommand RegisterAction { get; }
}
参数
internal class LoginModel
{
private string _UserName;
private string _Password;
public string UserName
{
get { return _UserName; }
set
{
_UserName = value;
}
}
public string Password
{
get { return _Password; }
set
{
_Password = value;
}
}
}
ICommand: 固定写法可照抄
public class RelayCommand : ICommand
{
private readonly Action _execute; // 要执行的操作(命令的主要功能)
private readonly Func<bool> _canExecute; // 用于判断命令是否可执行的方法
// ICommand 接口的事件,当命令的可执行状态发生变化时会触发该事件
public event EventHandler CanExecuteChanged;
// 构造函数,初始化 RelayCommand 实例
// 参数 execute:要执行的操作(必需)
// 参数 canExecute:用于判断命令是否可执行的方法(可选)
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
// 判断命令是否可执行的方法
// 参数 parameter:命令的参数(可选)
// 返回值:如果命令可以执行,返回 true,否则返回 false
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute();
}
// 执行命令的方法
// 参数 parameter:命令的参数(可选)
public void Execute(object parameter)
{
_execute();
}
}
当一个笔记 如有不足欢迎指正&&评论