benxu.AppPlatform.MAUI.Bootstrap 3.0.2

dotnet add package benxu.AppPlatform.MAUI.Bootstrap --version 3.0.2
                    
NuGet\Install-Package benxu.AppPlatform.MAUI.Bootstrap -Version 3.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="benxu.AppPlatform.MAUI.Bootstrap" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="benxu.AppPlatform.MAUI.Bootstrap" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="benxu.AppPlatform.MAUI.Bootstrap" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add benxu.AppPlatform.MAUI.Bootstrap --version 3.0.2
                    
#r "nuget: benxu.AppPlatform.MAUI.Bootstrap, 3.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package benxu.AppPlatform.MAUI.Bootstrap@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=benxu.AppPlatform.MAUI.Bootstrap&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=benxu.AppPlatform.MAUI.Bootstrap&version=3.0.2
                    
Install as a Cake Tool

benxu.AppPlatform.MAUI.Bootstrap

Bootstrap 套件 - 提供 Fluent API 進行一鍵服務註冊和生命週期管理。

特色

  • 一行程式碼註冊所有平台服務
  • Fluent API 設計,易於配置
  • 自動管理應用程式狀態
  • 內建生命週期管理
  • 延遲註冊模式

安裝

dotnet add package benxu.AppPlatform.MAUI.Bootstrap

使用範例

基本使用

在您的 MauiProgram.cs 中:

using benxu.AppPlatform.MAUI.Bootstrap.Extensions;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();

        builder.UseMauiApp<App>();
        builder.Services.AddMauiBlazorWebView();

        // 一鍵註冊所有平台服務
        builder.UseAppPlatform(options =>
        {
            // 使用 LiteDB 本地儲存
            options.UseLiteDB();

            // 使用 Firebase Auth
            options.UseFirebaseAuth();

            // 使用 FCM 推播
            options.UseFcm();
            
            // 使用 Firestore 資料庫
            options.UseFirestore();

            // 使用 Blazor UI 元件
            options.UseBlazorUI();
        });

        // 註冊您自己的服務
        builder.Services.AddScoped<IMyService, MyService>();

        return builder.Build();
    }
}

自訂設定

builder.UseAppPlatform(options =>
{
    // 自訂 LiteDB 設定
    options.UseLiteDB(db =>
    {
        db.DatabaseName = "myapp.db";
        db.EnableEncryption = true;
        db.EncryptionPassword = "your-password";
    });

    // 自訂 Firebase Auth 設定
    options.UseFirebaseAuth(auth =>
    {
        auth.EnableAutoTokenRefresh = true;
    });

    // 自訂 FCM 設定
    options.UseFcm(fcm =>
    {
        fcm.EnableForegroundNotifications = true;
    });
    
    // 自訂 Firestore 設定
    options.UseFirestore(fs =>
    {
        fs.EnableOfflinePersistence = true;
    });
});

使用生命週期管理

在您的 App.xaml.cs 中:

using benxu.AppPlatform.MAUI.Bootstrap.Lifecycle;

public partial class App : Application
{
    private readonly IAppLifecycleManager _lifecycleManager;

    public App(IAppLifecycleManager lifecycleManager)
    {
        InitializeComponent();
        _lifecycleManager = lifecycleManager;

        MainPage = new AppShell();
    }

    protected override async void OnStart()
    {
        // App 啟動時載入狀態
        await _lifecycleManager.OnAppStartAsync();
    }

    protected override async void OnSleep()
    {
        // App 休眠時持久化狀態
        await _lifecycleManager.OnAppSleepAsync();
    }

    protected override async void OnResume()
    {
        // App 恢復時檢查狀態
        await _lifecycleManager.OnAppResumeAsync();
    }
}

使用狀態管理

在您的 Blazor 頁面或服務中:

@inject IAppStateManager StateManager

@code {
    protected override void OnInitialized()
    {
        // 訂閱狀態變更事件
        StateManager.StateChanged += OnStateChanged;

        // 讀取當前狀態
        var currentState = StateManager.CurrentState;
        var isAuthenticated = currentState.Auth.IsAuthenticated;
        var fcmToken = currentState.FcmToken;
    }

    private void OnStateChanged(object? sender, AppStateChangedEventArgs e)
    {
        // 狀態改變時更新 UI
        InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        StateManager.StateChanged -= OnStateChanged;
    }
}

登出時清理狀態

@inject IStateCleanupService CleanupService

private async Task SignOutAsync()
{
    // 清理所有狀態(解綁 FCM、清除認證)
    await CleanupService.CleanupOnSignOutAsync();

    // 導航到登入頁面
    Navigation.NavigateTo("/login");
}

核心概念

AppStateManager

全域應用程式狀態管理器,負責:

  • 管理認證狀態(AuthState)
  • 管理 FCM Token
  • 持久化狀態到本地儲存
  • 觸發狀態變更事件

AppLifecycleManager

應用程式生命週期管理器,負責:

  • App 啟動時載入狀態
  • App 休眠時持久化狀態
  • App 恢復時檢查狀態

StateCleanupService

狀態清理服務,負責:

  • 登出時清理所有狀態
  • App 終止時持久化狀態

Fluent API

可用的模組

  • UseLiteDB() - 註冊 LiteDB 本地儲存 ✅
  • UseFirebaseAuth() - 註冊 Firebase 認證 ✅
  • UseFcm() - 註冊 Firebase 推播 ✅
  • UseFirestore() - 註冊 Firestore 資料庫 ✅
  • UseBlazorUI() - 註冊 Blazor UI 元件 ✅

架構優勢

1. 簡化的 DX(開發者體驗)

只需一行程式碼即可註冊所有服務:

builder.UseAppPlatform(options => { ... });

2. 延遲註冊模式

模組化設計,只註冊您需要的服務:

// 只使用 LiteDB
options.UseLiteDB();

// 或使用完整平台
options.UseLiteDB()
       .UseFirebaseAuth()
       .UseFcm()
       .UseFirestore()
       .UseBlazorUI();

3. 自動狀態管理

無需手動管理狀態持久化和載入:

// 自動載入
await _lifecycleManager.OnAppStartAsync();

// 自動持久化
await _lifecycleManager.OnAppSleepAsync();

4. 事件驅動

訂閱狀態變更事件,自動更新 UI:

StateManager.StateChanged += (s, e) => {
    // 狀態改變時自動執行
};

依賴項目

此套件依賴:

  • benxu.AppPlatform.Core - 核心抽象層
  • benxu.AppPlatform.Storage.LiteDB - LiteDB 儲存實作
  • Microsoft.Maui.Controls - MAUI 框架
  • Microsoft.Extensions.DependencyInjection.Abstractions - DI 抽象

授權

MIT License - Copyright (c) 2025 benxu

Product Compatible and additional computed target framework versions.
.NET net10.0-android36.0 is compatible.  net10.0-ios26.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 81 1/17/2026
3.0.0 89 1/13/2026
2.0.0 91 1/11/2026
1.0.0 105 12/28/2025