1 新建wpf应用
2 新建Views,Models,ViewModels文件夹
3 新建Base文件夹,并在其里面新建CommandBase类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfApp1.Base
{
public class CommandBase : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
DoExecute?.Invoke();
}
public Action DoExecute { get; set; }
public CommandBase(Action doExecute)
{
this.DoExecute = doExecute;
}
}
}
3 在ViewModels里面新建MainViewModels类,并在其里面新建MainViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfApp1.Base;
using WpfApp1.

本文档展示了如何在WPF应用程序中使用MVVM模式创建一个简单的UI,其中包括新建项目结构、定义自定义命令基类CommandBase、创建ViewModel以及在MainWindow中绑定命令到Button,最终实现按钮点击事件的处理。
最低0.47元/天 解锁文章
202

被折叠的 条评论
为什么被折叠?



