• 将Abp移植进.NET MAUI项目(一):搭建项目


    前言

    写在.NET MAUI官宣正式发布之际,热烈庆祝MAUI正式发布!

    去年12月份做了MAUI混合开发框架的调研,想起来文章里给自己挖了个坑,要教大家如何把Abp移植进Maui项目,由于篇幅限制,将分为三个章节。

    将Abp移植进.NET MAUI项目(一):搭建项目 - 林晓lx - 博客园 (cnblogs.com)

    将Abp移植进.NET MAUI项目(二):配置与基类编写 - 林晓lx - 博客园 (cnblogs.com)

    将Abp移植进.NET MAUI项目(三):构建UI层 - 林晓lx - 博客园 (cnblogs.com)

    熟悉Abp的同学都知道,Abp 是一套强大的应用程序设计时框架(俗称脚手架),新版本的Abp vNext为微服务和网络优化的更多,然而本地开发经典Abp已经够用,而且官方没有停止维护,因此使用这个框架

    MAUI则是跨平台的应用程序抽象层,强大的运行时框架 + 强大的设计时框架 , 我说这是宇宙最强大跨平台开发框架,不为过吧?😁

    计划:

    • 整个程序我们还是利用Mvvm设计模式,但是将利用Abp的Ioc容器,而不使用mvvmlight或者xamarinToolkit这些库,自行编写一个ViewModelBase
    • 使用Abp.EntityFrameworkCore库中的EF相关功能,使用sqlite作为数据持久化方案。

    目标:编写一个歌单App,对歌曲信息进行增、删、查、改。

    下面来看看如何搭建

    搭建MAUI项目

    请注意:本文发布时,MAUI处于RC3版本,仍没有正式发布,需要安装Visual Studio 2022 17.3 (Preview)

    首先按照官方教程搭建一个MAUI项目, 命名为MauiBoilerplateBuild your first .NET MAUI app - .NET MAUI | Microsoft Docs

    再前往Abp官网生成一个项目 
    Startup Templates - Create a Demo | AspNet Boilerplate

    • 选择最新版本 v7.x 和.Net 6版本
    • 取消勾选“Include login, register, user, role and tenant management pages”
    • 项目名称中填入MauiBoilerplate与Maui项目保持一致


    点击“Create My Project”生成abp项目文件,等待下载完成

    下载,解压好后,打开src目录可以发现4个项目目录,我们仅需要Core和EntityFrameworkCore项目,将这两个目录移至项目根目录,并且添加至解决方案。


     

    配置应用入口点

    在MauiBoilerplate.Core项目中

    改写默认配置文件

    {
      "ConnectionStrings": {
        "Default": "Data Source=file:{0};"
      },
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Debug",
          "System": "Information",
          "Microsoft": "Information"
        }
      }
    }
    

    在MauiBoilerplate.Core.csproj中的ItemGroup节点下添加

    	  <EmbeddedResource Include="appsettings.json">
    	    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    	  </EmbeddedResource>

     

    在MauiBoilerplate.Core项目中新建MauiBoilerplateBuilderExtensions.cs 作为程序入口

    添加一个静态方法InitConfig,用于读取项目的配置文件appsettings.json,若第一次运行或者该文件不存在则读取默认的配置文件

            private static void InitConfig(string logCfgName, string documentsPath)
            {
    
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MauiBoilerplateBuilderExtensions)).Assembly;
    
                Stream stream = assembly.GetManifestResourceStream($"MauiBoilerplate.Core.{logCfgName}");
                string text = "";
                using (var reader = new System.IO.StreamReader(stream))
                {
                    text = reader.ReadToEnd();
                }
                if (DirFileHelper.IsExistFile(documentsPath))
                {
                    var currentFileContent = DirFileHelper.ReadFile(documentsPath);
                    var isSameContent = currentFileContent.ToMd5() == text.ToMd5();
                    if (isSameContent)
                    {
                        return;
                    }
                    DirFileHelper.CreateFile(documentsPath, text);
    
                }
                else
                {
                    DirFileHelper.CreateFile(documentsPath, text);
    
                }
            }
    

     

    添加一个静态方法InitDataBase用于初始化sqlite数据库文件"mato.db"

        private static void InitDataBase(string dbName, string documentsPath)
        {
                var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MauiBoilerplateBuilderExtensions)).Assembly;
                Stream stream = assembly.GetManifestResourceStream($"MauiBoilerplate.Core.{dbName}");
                StreamHelper.WriteStream(stream, documentsPath);
    
                var path = Path.GetDirectoryName(documentsPath);
                DirFileHelper.CreateDir(path);
        }

    添加一个 静态方法UseMauiBoilerplate用于初始化配置文件,初始化db文件和向管道服务中注册AbpBootstrapper实例。

            public static MauiAppBuilder UseMauiBoilerplate<TStartupModule>(this MauiAppBuilder builder) where TStartupModule : AbpModule
            {
                var logCfgName = "log4net.config";
                var appCfgName = "appsettings.json";
                var dbName = "mato.db";
    
                string documentsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), MauiBoilerplateConsts.LocalizationSourceName, logCfgName);
                string documentsPath2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), MauiBoilerplateConsts.LocalizationSourceName, appCfgName);
                string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), MauiBoilerplateConsts.LocalizationSourceName, dbName);
    
                InitConfig(logCfgName, documentsPath);
                InitConfig(appCfgName, documentsPath2);
                InitDataBase(dbName, dbPath);
                var _bootstrapper = AbpBootstrapper.Create<TStartupModule>(options =>
                {
                    options.IocManager = new IocManager();
                });
                _bootstrapper.IocManager.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig(documentsPath));
    
                builder.Services.AddSingleton(_bootstrapper);
                WindsorRegistrationHelper.CreateServiceProvider(_bootstrapper.IocManager.IocContainer, builder.Services);
    
                return builder;
            }
    

    在MauiBoilerplate项目中

    新建MauiBoilerplateModule.cs ,并编写代码如下,这是App起始模块

    [DependsOn(typeof(MauiBoilerplateEntityFrameworkCoreModule))]
        public class MauiBoilerplateModule : AbpModule
        {
            public override void Initialize()
            {
                IocManager.RegisterAssemblyByConvention(typeof(MauiBoilerplateModule).GetAssembly());
            }
    
        }

    打开MauiProgram.cs文件,将UseMauiBoilerplate添加到MauiAppBuilder

    这里提一下, MAUI 应用跟其他.Net6应用一样采用泛型主机启动应用,在项目中有一个静态MauiProgram类,这是应用的入口点。 这提供了从单个位置配置应用、服务和第三方库的功能。

    更多泛型主机的信息,请参阅微软文档.NET 通用主机 | Microsoft Docs


     至此,在主机管道中已经配置了MauiBoilerplate服务

    配置Abp

    App.xaml是应用的声明起始点,将从这里初始化Abp

    打开App.xaml.cs,添加如下代码:

    public partial class App : Application
        {
            private readonly AbpBootstrapper _abpBootstrapper;
    
            public App(AbpBootstrapper abpBootstrapper)
            {
                _abpBootstrapper = abpBootstrapper;
                InitializeComponent();
                _abpBootstrapper.Initialize();
                this.MainPage = abpBootstrapper.IocManager.Resolve(typeof(MainPage)) as MainPage;
            }
        }

    注意,我们还没有创建初始页面MainPage,你可以先创建这个文件,将在第三章讲UI层时介绍

    至此,就完成了MAUI项目的搭建与Abp脚手架的集成,现在你可以在这个项目中使用Abp的IocManager,ConfigurationManager,工作单元特性,模组化特性,等等任何的Abp提供的功能了。

    但是距离目标:制作一个具有数据访问层的App,还需要两段路要走:配置数据库,以及编写界面。

    请看下一章将Abp移植进.NET MAUI项目(二):配置与基类编写 - 林晓lx - 博客园 (cnblogs.com)

     项目地址

    jevonsflash/maui-abp-sample (github.com)


  • 相关阅读:
    【SpringBoot】YAML 配置文件
    技术分享| anyRTC音视频混流技术解析
    【Django】Django4.1.2使用xadmin避坑指南(二)
    C#上位机系列(3)—定时器和串口的介绍
    vue-ant design示例大全——按钮本地css/js资源
    iPhone垃圾清理器:AnyMP4 iOS Cleaner for mac
    not in后面集合有null会导致主查询就查不到记录
    Java互联网+公立医院绩效考核源码
    短信上行和短信下行
    微信群BUG大揭秘!开启身份切换神器
  • 原文地址:https://www.cnblogs.com/jevonsflash/p/16310387.html