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}";
相关推荐
Aevget38 分钟前
DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(二)?
wpf·devexpress·.net 10·data grid
没有bug.的程序员2 天前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc
Macbethad2 天前
基于WPF的半导体设备配方管理程序技术方案
wpf
FuckPatience2 天前
WPF Geometry
wpf
武藤一雄3 天前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
MarkHD3 天前
车辆TBOX科普 第70次 AUTOSAR Adaptive、容器化与云原生的融合革命
云原生·wpf
极客智造3 天前
WPF Behavior 实战:自定义 InvokeCommandAction 实现事件与命令解耦
wpf
L、2183 天前
Flutter 与 OpenHarmony 深度集成:构建分布式多端协同应用
分布式·flutter·wpf
布伦鸽3 天前
C# WPF -MaterialDesignTheme 找不到资源“xxx“问题记录
开发语言·c#·wpf
小二·4 天前
MyBatis基础入门《十五》分布式事务实战:Seata + MyBatis 实现跨服务数据一致性
分布式·wpf·mybatis