將Abp移植進.NET MAUI專案(一):搭建專案

林曉lx發表於2022-05-25

 前言

去年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專案保持一致

將Abp移植進.NET MAUI專案(一):搭建專案編輯

點選“Create My Project”生成abp專案檔案,等待下載完成

下載,解壓好後,開啟src目錄可以發現4個專案目錄,我們僅需要Core和EntityFrameworkCore專案,將這兩個目錄移至專案根目錄,並且新增至解決方案。

將Abp移植進.NET MAUI專案(一):搭建專案編輯

 

 

配置應用入口點

在MauiBoilerplate.Core專案中

改寫預設配置檔案

{
  "ConnectionStrings": {
    "Default": "Data Source=file:{0};"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}
將Abp移植進.NET MAUI專案(一):搭建專案

在MauiBoilerplate.Core.csproj中的ItemGroup節點下新增

	  <EmbeddedResource Include="appsettings.json">
	    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
	  </EmbeddedResource>
將Abp移植進.NET MAUI專案(一):搭建專案

 

在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);

            }
        }
將Abp移植進.NET MAUI專案(一):搭建專案

 

新增一個靜態方法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);
    }
將Abp移植進.NET MAUI專案(一):搭建專案

新增一個 靜態方法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;
        }
將Abp移植進.NET MAUI專案(一):搭建專案

在MauiBoilerplate專案中

新建MauiBoilerplateModule.cs ,並編寫程式碼如下,這是App起始模組

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

    }
將Abp移植進.NET MAUI專案(一):搭建專案

開啟MauiProgram.cs檔案,將UseMauiBoilerplate新增到MauiAppBuilder

這裡提一下, MAUI 應用跟其他.Net6應用一樣採用泛型主機啟動應用,在專案中有一個靜態MauiProgram類,這是應用的入口點。 這提供了從單個位置配置應用、服務和第三方庫的功能。

更多泛型主機的資訊,請參閱微軟文件.NET 通用主機 | Microsoft Docs

將Abp移植進.NET MAUI專案(一):搭建專案編輯

 至此,在主機管道中已經配置了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;
        }
    }
將Abp移植進.NET MAUI專案(一):搭建專案

注意,我們還沒有建立初始頁面MainPage,你可以先建立這個檔案,將在第三章講UI層時介紹

至此,就完成了MAUI專案的搭建與Abp腳手架的整合,現在你可以在這個專案中使用Abp的IocManager,ConfigurationManager,工作單元特性,模組化特性,等等任何的Abp提供的功能了。

但是距離目標:製作一個具有資料訪問層的App,還需要兩段路要走:配置資料庫,以及編寫介面。

請看下一章將Abp移植進.NET MAUI專案(二):配置與基類編寫 - 林曉lx - 部落格園 (cnblogs.com)

 專案地址

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


相關文章