C#WPF使用CommunityToolkit.Mvvm库

C#WPF之快速理解MVVM模式

接上篇介绍MVVM,这里使用相关库介绍MVVM。

CommunityToolkit.Mvvm

CommunityToolkit.Mvvm介绍

CommunityToolkit.Mvvm是Microsoft Community Toolkit的一部分,它是一个轻量级但功能强大的MVVM(Model-View-ViewModel)库,旨在帮助开发者更容易地实现MVVM设计模式。

该库提供了一些基础类,如ObservableObjectObservableRecipient,这些类实现了INotifyPropertyChanged接口,并提供了SetProperty方法,可以在属性值改变时触发PropertyChanged事件。这使得数据绑定变得更加简单和高效。

此外,该库还提供了ICommand接口的实现,如RelayCommandAsyncRelayCommand,这些类可以帮助你创建命令,命令是MVVM模式中的一个重要组成部分。

CommunityToolkit.Mvvm还提供了一些其他有用的特性,如消息传递、设计时数据支持等,这些特性可以帮助你更好地组织和管理你的代码。

CommunityToolkit.Mvvm是一个强大的工具,它可以帮助你更容易地实现MVVM模式,从而提高你的代码质量和开发效率。

修改之后的ViewModel如下所示:

cs 复制代码
 public partial class MainViewModel :ObservableObject
 {
     public ObservableCollection<User> Users { get; set; }
     public ICommand AddUserCommand { get; set; }
     public ICommand TestCommand { get; set; }
     [ObservableProperty]
     private string? _name;
     /* public string? Name
      {
          get { return _name; }
          set
          {
              if (_name != value)
              {
                  _name = value;
                  OnPropertyChanged(nameof(Name));
              }
          }
      }*/
     public int MyProperty { get; set; }
     [ObservableProperty]
     private string? _email;
    /* public string? Email
     {
         get { return _email; }
         set
         {
             if (_email != value)
             {
                 _email = value;
                 OnPropertyChanged(nameof(Email));
             }
         }
     }*/
     public MainViewModel()
     {
         Users = UserManager.GetUsers();
         AddUserCommand = new Commands.RelayCommand(Adduser, CanAddUser);
         TestCommand = new Commands.RelayCommand(TestAction, CanTest);

     }

     public event PropertyChangedEventHandler? PropertyChanged;
     protected virtual void OnPropertyChanged(string propertyName)
     {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

     }
     [RelayCommand]
     private void Adduser(object obj)
     {
         User user = new User();
         user.Name = Name;
         user.Email = Email;
         UserManager.AddUser(user);
     }
     private bool CanAddUser(object obj)
     {
         return true;
     }
     private bool CanTest(object obj)
     {
         return true;
     }
     [RelayCommand]
     private void TestAction(object obj)
     {
         Users[0].Name = "demo";
         Users[0].Email = "1130@qq.com";
         //Name = "demo";
         //Email = "1130@qq.com";
     }
 }

修改之后的User类如下所示:

cs 复制代码
   public partial class User:ObservableObject
   {
       [ObservableProperty]
       private string? _name;
       /* public string? Name 
        {
            get { return _name; }

            set 
            { 
                if(_name!= value)
                {
                    _name = value;
                    OnPropertyChanged(nameof(Name));
                }
            }
        }
*/
       [ObservableProperty]
       private string? _email;
       /*public string? Email
       {
           get { return _email; }

           set
           {
               if (_email != value)
               {
                   _email = value;
                   OnPropertyChanged(nameof(Email));
               }
           }
       }*/
       public event PropertyChangedEventHandler? PropertyChanged;
       protected virtual void OnPropertyChanged(string propertyName)
       {
           PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
       }
   }

现在我们的ViewModel与Model就可以简化了。

MVVM模式,创建实现ICommand接口的RelayCommand类,实现INotifyPropertyChanged接口的MainViewModel类与User类。使用数据绑定与命令绑定改写xaml页面。

最后手动实现MVVM模式,需要写很多代码,比较复杂与麻烦,我们使用MVVM库来简化MVVM模式的实现。

相关推荐
Vacant Seat33 分钟前
图论-实现Trie(前缀树)
java·开发语言·数据结构·图论
猪猪虾的业余生活41 分钟前
Qt 驾校考试系统项目实现
开发语言·qt
香菇滑稽之谈43 分钟前
责任链模式的C++实现示例
开发语言·c++·设计模式·责任链模式
风莫寻1 小时前
【Troubleshot】Qt 长按按键 keyPressEvent keyReleaseEvent 自动重复问题
开发语言·qt
ZC·Shou1 小时前
Rust 之一 基本环境搭建、各组件工具的文档、源码、配置
开发语言·rust·cargo·rustc·rustup·clippy·rustfmt
Hello.Reader1 小时前
深入理解 Rust 中的模式匹配语法
开发语言·rust
最胖的小仙女1 小时前
通过动态获取后端数据判断输入的值打小
开发语言·前端·javascript
阿波拉1 小时前
AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas’问题解决
开发语言·python
臣妾写不来啊2 小时前
使用dify的api连接外部知识库,dify连接ragflow的知识库(附java代码)
java·开发语言·spring boot
hhw1991122 小时前
C#面试题整理11
c#