WPF CommunityToolkit.Mvvm

文章目录

前言

CommunityToolkit.Mvvm(以下简称Toolkit)是WPF最有名的两个框架,一个是Prism,另一个就是Toolkit。

Prism可以看我的Prism详解

WPF Prims框架详解

Toolkit

Toolkit 官方文档

用 CommunityToolkit.Mvvm 加速 MVVM 开发流程

Nuget安装

简单使用

Toolkit简单复写了我们常用的两个方法

一个是 SetProperty,一个是RelayCommand

SetProperty,通知更新

csharp 复制代码
 public class MainViewModel:ObservableObject
 {
     private string _title;

     public string Title
     {
         get => _title;
         set => SetProperty(ref _title,value);
     }
 }

RealyCommand

csharp 复制代码
public RelayCommand ButtonClickCommand { get; set; }

public MainViewModel()
{
    ButtonClickCommand = new RelayCommand(() => { Debug.WriteLine("Hello World!"); });

}

MVVM模式的3种command总结[2]--RelayCommand

RealyCommand主要有一个CanExecute属性。通知是否能点击

CanExecute

通知按钮能否点击(我感觉有点鸡肋)

csharp 复制代码
public class MainViewModel:ObservableObject
{
    private string _title = "Hello world!";

    public string Title
    {
        get => _title;
        set => SetProperty(ref _title,value);
    }

    private bool _isEnable = false;

    public bool IsEnable
    {
        get=> _isEnable;
        set
        {
            SetProperty(ref _isEnable,value);
            ButtonClickCommand.NotifyCanExecuteChanged();
        }
    }




    public RelayCommand ButtonClickCommand { get; set; }


    public MainViewModel()
    {
        ButtonClickCommand = new RelayCommand(() => { Title = "Value is changed"; },()=>IsEnable);

    }
}
xml 复制代码
<Window.DataContext>
    <viewModel:MainViewModel />
</Window.DataContext>
<Grid>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
        <TextBox Text="{Binding Title}" Width="200"/>
        <CheckBox Content="Is Enable" IsChecked="{Binding IsEnable}" />
        <Separator Margin="5"/>
        <Button Command="{Binding ButtonClickCommand}" Content="Click Me"/>
    </StackPanel>
</Grid>

新功能,代码生成器

Toolkit新增了生成器功能,自动帮我们将代码补全。

ObservableProperty

ObservableProperty

生成的Public属性是符合一下特性

  • private
    • name=>Name
    • _name=>Name
    • m_name=>Name

以上三者都会自动对应到public Name

NotifyCanExecuteChangedFor

因为我们之前修改RelayCommand的CanExecute都是通过public 里面的get set去通知的,我们现在可以使用NotifyCanExecuteChangedFor来通知

csharp 复制代码
[NotifyCanExecuteChangedFor(nameof(ButtonClickCommand))]
[ObservableProperty]
private bool _isEnable = false;
//等价于
private bool _isEnable = false;

    public bool IsEnable
    {
        get=> _isEnable;
        set
        {
            SetProperty(ref _isEnable,value);
            ButtonClickCommand.NotifyCanExecuteChanged();
        }
    }

RelayCommand

RelayCommand给一个Void函数,会自动生成一个对应Command 和初始化这个Command

csharp 复制代码
[RelayCommand]
public void ButtonClick()
{

}
//等价于
public RelayCommand ButtonClickCommand { get; set; }


public MainViewModel()
{
    ButtonClickCommand = new RelayCommand(() => { Title = "Value is changed"; }, () => IsEnable);

}

其他功能

csharp 复制代码
//将CanExecute绑定到IsEnable
[RelayCommand(CanExecute =nameof(IsEnable))]
public void ButtonClick()
{

}
///异步函数也可以,CanExecute会在异步函数结束之后变回去
[RelayCommand(CanExecute =nameof(IsEnable))]
public async Task ButtonClickAsync()
{
    await Task.Delay(1000);
    Title = "我被修改了";
}

异步函数演示:注意看按钮颜色

对应关系

ButtonClickAsync、ButtonClick=>ButtonClickCommand

NotifyPropertyChangedFor

我们希望两个属性是强关联的,比如Title和TestStr是强关联的。

我们希望能通知另一个属性发生了更改,比如Title 通知TestStr更改

csharp 复制代码
 private string testStr = $"Title:{Title}";

但是这么写会报错,只能设置静态方法

然后我们可以通过NotifyPropertyChangedFor来进行通知

csharp 复制代码
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(TestStr))]
private string _title = "Hello world!";

public string TestStr => $"Title:{Title}";
相关推荐
cn_mengbei13 小时前
鸿蒙PC原生应用开发实战:ArkTS与DevEco Studio从零构建跨端桌面应用全栈指南
华为·wpf·harmonyos
lingxiao1688820 小时前
WebApi详解+Unity注入--上篇:基于Framework的WebApi
c#·wpf·web
是一个Bug1 天前
Java后端开发面试题清单(50道) - 分布式基础
java·分布式·wpf
无心水1 天前
【分布式利器:腾讯TSF】4、TSF配置中心深度解析:微服务动态配置的终极解决方案
分布式·微服务·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
lingxiao168881 天前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
无心水2 天前
【分布式利器:腾讯TSF】6、TSF可观测性体系建设实战:Java全链路Metrics+Tracing+Logging落地
java·分布式·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
故事不长丨3 天前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对
冰茶_3 天前
WPF路由事件:隧道与冒泡机制解析
学习·c#·.net·wpf·.netcore·mvvm
He BianGu3 天前
【笔记】 WPF中CollectionChangedEventManager功能详细介绍
笔记·wpf
张人玉3 天前
C#WPF页面布局及其属性
开发语言·c#·wpf