乘风破浪,遇见最佳跨平台跨终端框架.Net Core/.Net生态 - WPF应用整合依赖注入(DI)、MediatR、CommunityToolkit.Mvvm

前言

之前一直用Stylet,写过两篇乘风破浪,遇见Stylet超清爽WPF御用MVVM框架,爱不释手的.Net Core轻量级MVVM框架乘风破浪,超清爽WPF御用MVVM框架Stylet,启动到登录设计的高阶实战,用这确实很爽,在MVVM这块非常省心,用起来有点在写UWP的感觉。

但是这个玩意自带了IOC,我又想引入MediatR来实现面向消息事件的编程(这个做客户端的同学可能不熟悉哈哈,一般大家都是面向接口编程),它只针对.Net Core原汁原味的DI有扩展支持,所以不得不最终弃用Stylet

离开了Stylet,我就没有了MVVM的配套支持,怎么办呢?想起了之前写的乘风破浪,遇见MVVM Toolkit官方社区首推MVVM框架,后UWP时代的拯救版MVVM框架,这是有社区维护的一个MVVM框架,用它肯定没错了,牛逼的是,微软改名部这么快就把它从Microsoft.Toolkit.Mvvm改名为CommunityToolkit.Mvvm了。

image

所以最终的组合是:

  • .Net 6
  • Microsoft.Extensions.DependencyInjection
  • MediatR.Extensions.Microsoft.DependencyInjection
  • CommunityToolkit.Mvvm
  • PropertyChanged.Fody

引入官方依赖注入(DI)

https://github.com/TaylorShi/HelloNetWPF

准备示例项目

这个倒是不难,这里我们使用.Net 6/5/3.1来创建WPF应用哈。

image

image

image

引入依赖包

依赖包

https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection

dotnet add package Microsoft.Extensions.DependencyInjection

image

基本使用

刚创建完的项目模板中,App.Xaml.cs中很简单,啥都没有。

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

我们准备一个熟悉的函数ConfigureServices来得到IServiceProvider

private static IServiceProvider ConfigureServices()
{
    var services = new ServiceCollection();

    return services.BuildServiceProvider();
}

这里我们利用它的构造函数来调用这个函数

public IServiceProvider Services { get; }

public App()
{
    InitializeComponent();
    Services = ConfigureServices();
}

这里定义一个Services对象来托管住IServiceProvider,方便后面做个全局使用。

基于DI实现页面窗体展示

注册窗体到容器中

既然有了DI,那我们就要用起来,把窗体丢进去,从容器中拿出来用。

private static IServiceProvider ConfigureServices()
{
    var services = new ServiceCollection();
    services.AddSingleton<MainWindow>();
    return services.BuildServiceProvider();
}

这里把MainWindow页面做个单实例注入。

使用容器中窗体

我们在定一个启动函数OnStartup,把它取出来,并且展示出来。

private void OnStartup(object sender, StartupEventArgs e)
{
    var mainWindow = Services.GetService<MainWindow>();
    mainWindow?.Show();
}

改造启动入口

上哪去调用这个启动函数OnStartup呢?我们去App.xaml.cs对应的App.xaml改动下。

改动前,它是使用StartupUri直接导向MainWindow.xaml的,这里我们把它换成Startup函数指定。

<Application x:Class="demoForWpfApp60.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:demoForWpfApp60"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
<Application x:Class="demoForWpfApp60.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:demoForWpfApp60"
             Startup="OnStartup">
    <Application.Resources>

    </Application.Resources>
</Application>

这样就使用了容器来管理界面了。

image

.Net 5或者.Net Core 3.1也可以

虽然在.Net 5.Net Core 3.1会遇到一些不友好的警告提示,但是还是能成功运行起来。

Microsoft.Extensions.DependencyInjection.Abstractions 7.0.0 doesn't support net5.0-windows and has not been tested with it. Consider upgrading your TargetFramework to net6.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
Microsoft.Extensions.DependencyInjection.Abstractions 7.0.0 doesn't support netcoreapp3.1 and has not been tested with it. Consider upgrading your TargetFramework to net6.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.

image

引入Mvvm支持

引入CommunityToolkit.Mvvm包

依赖包

CommunityToolkit.Mvvm最低也要.Net 6了,其的前身就是Microsoft.Toolkit.Mvvm,如果是.Net 5或者.NET Standard 2.0可以考虑优先使用Microsoft.Toolkit.Mvvm

image

dotnet add package CommunityToolkit.Mvvm

image

dotnet add package Microsoft.Toolkit.Mvvm

添加ViewModel

创建一个简单的ViewModel示例: MainViewModel,它继承了ObservableObject,这是CommunityToolkit.Mvvm中一个通过实现INotifyPropertyChangedINotifyPropertyChanging接口可观察的对象的基类。它可以用作需要支持属性更改通知的各种对象的起点。

internal class MainViewModel : ObservableObject
{

}
namespace CommunityToolkit.Mvvm.ComponentModel;

/// <summary>
/// A base class for objects of which the properties must be observable.
/// </summary>
public abstract class ObservableObject : INotifyPropertyChanged, INotifyPropertyChanging
{
}

然后在容器中注册它,这里我们把它注册为瞬时模式

private static IServiceProvider ConfigureServices()
{
    var services = new ServiceCollection();
    services.AddTransient<MainViewModel>();
    services.AddSingleton<MainWindow>();
    return services.BuildServiceProvider();
}

同时,我们构建一个静态变量来托管当前的App实例。

public partial class App : Application
{
    public new static App Current => (App)Application.Current;
}

然后在View界面使用它作为数据上下文

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = App.Current.Services.GetService<MainViewModel>();
    }
}

这样就可以了,当前MainViewModelMainView就对应起来了。

添加简单绑定

接下来我们在MainViewModel添加简单属性和事件及命令。

internal class MainViewModel : ObservableObject
{
    private int _count;
    public int Count
    {
        get => _count;
        set => SetProperty(ref _count, value);
    }

    public ICommand AddCountCommand => new RelayCommand(AddCount);

    public void AddCount()
    {
        Count++;
    }
}

在界面上我们构建一个按钮吧

<Window x:Class="demoForWpfApp60.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:demoForWpfApp60"
        mc:Ignorable="d"
        Title="demoForWpfApp60" Height="450" Width="800">
    <Grid>
        <Button Content="{Binding Count}" Command="{Binding AddCountCommand}"/>
    </Grid>
</Window>

就这么简单,就可以运行起来了。

image

支持异步的命令绑定

public IAsyncRelayCommand RemoveCountCommand => new AsyncRelayCommand(RemoveCount);

public async Task RemoveCount()
{
    await Task.Run(() =>
    {
        Count--;
    });
}
<Window x:Class="demoForWpfApp60.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:demoForWpfApp60"
        mc:Ignorable="d"
        Title="demoForWpfApp60" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button 
            Grid.Row="0"
            Content="{Binding Count}"
            Command="{Binding AddCountCommand}"
            />
        <Button 
            Grid.Row="1" 
            Content="{Binding Count}"
            Command="{Binding RemoveCountCommand}"
            />
    </Grid>
</Window>

image

更优雅的通知界面

之前我们写一个可观察且通知界面的属性是这样写的。

private int _count;
public int Count
{
    get => _count;
    set => SetProperty(ref _count, value);
}

能不能更优雅一点呢,可以,引入PropertyChanged.Fody包,它会自动在编译时给已知属性注入IL代码,以达到PropertyChanged通知的效果。

https://www.nuget.org/packages/PropertyChanged.Fody

dotnet add package PropertyChanged.Fody

直接写成

public int Count { get; set; }

它会帮我们在编译的时候,自动生成前面那种代码,省去了重复代码量。

引入MediatR支持

添加MediaR包

有了官方DI,添加对MediatR的支持就简单了。

依赖包

https://www.nuget.org/packages/MediatR.Extensions.Microsoft.DependencyInjection

dotnet add package MediatR.Extensions.Microsoft.DependencyInjection

基本使用

通过程序集扫描来注入MediatR相关服务。

private static IServiceProvider ConfigureServices()
{
    var services = new ServiceCollection();
    services.AddMediatR(typeof(MainViewModel).Assembly);
    return services.BuildServiceProvider();
}

创建命令和命令处理

internal class RemoveCountCommand: IRequest<int>
{
    public int Count { get; private set; }

    public RemoveCountCommand(int count) { this.Count = count; }
}

internal class RemoveCountCommandHandler : IRequestHandler<RemoveCountCommand, int>
{
    public async Task<int> Handle(RemoveCountCommand request, CancellationToken cancellationToken)
    {
        return await Task.FromResult(request.Count - 1);
    }
}

使用MediatR发送事件。

internal class MainViewModel : ObservableObject
{
    private int _count;
    public int Count
    {
        get => _count;
        set => SetProperty(ref _count, value);
    }

    private readonly IMediator _mediator;
    public MainViewModel(IMediator mediator)
    {
        _mediator = mediator;
    }

    public IAsyncRelayCommand RemoveCountCommand => new AsyncRelayCommand(RemoveCount);

    public async Task RemoveCount()
    {
        Count = await _mediator.Send(new RemoveCountCommand(Count));
    }
}

这样运行也是一样的,变成了中介者模式,面向消息事件编程了。

参考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章