將Abp移植進.NET 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)