mcpField
M
maui-blazor-development

maui-blazor-development

github.com/ibutters开发380 次安装46 周前更新
代码仓库

关于 maui-blazor-development

maui-blazor-development 是用于规划和构建跨平台 .NET MAUI Blazor 应用的开发技能,涵盖架构选择、原生平台功能、状态管理与导航模式。

主要功能

  1. 01规划平台与应用架构
  2. 02MAUI 和 Blazor 项目设置
  3. 03依赖注入模式
  4. 04访问原生功能
  5. 05生命周期、状态与导航指导

适用场景

  1. 01为移动端和共享 Web UI 选择模板
  2. 02集成设备 API 和权限
  3. 03选择状态管理与导航模式

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ibutters/claudecodeplugins --skill maui-blazor-development

安装后在 Claude Code 中输入/maui-blazor-development确认已加载。

详细介绍

.NET MAUI Blazor 混合开发

使用 .NET MAUI 和 Blazor 构建跨平台应用的专家指南。

实现工作流

1. 分析阶段(必需)

实现前,请确定:

  • 应用类型:纯 MAUI Blazor,或 MAUI + Blazor Web App(共享 UI)
  • 目标平台:Android、iOS、Windows、macOS
  • 所需原生功能:传感器、相机、文件系统、通知
  • 状态管理:组件状态、MVVM 或混合方式
  • 导航模式:仅使用 Blazor、Shell 或混合导航
2. 架构决策
场景推荐方式
移动端优先,兼顾部分 Webmaui-blazor 模板
在移动端和 Web 之间共享 UI使用 RCL 的 maui-blazor-web 模板
复杂的原生集成使用平台服务的 MVVM
简单的数据驱动 UI使用 DI 服务的组件状态
3. 项目设置
// MauiProgram.cs - Essential setup
public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        });

    builder.Services.AddMauiBlazorWebView();
#if DEBUG
    builder.Services.AddBlazorWebViewDeveloperTools();
#endif

    // Register services
    builder.Services.AddSingleton<IDeviceService, DeviceService>();
    builder.Services.AddScoped<IDataService, DataService>();

    return builder.Build();
}
4. BlazorWebView 配置

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:b="clr-namespace:Microsoft.AspNetCore.Components.WebView.Maui;assembly=Microsoft.AspNetCore.Components.WebView.Maui">
    <b:BlazorWebView HostPage="wwwroot/index.html">
        <b:BlazorWebView.RootComponents>
            <b:RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
        </b:BlazorWebView.RootComponents>
    </b:BlazorWebView>
</ContentPage>

核心模式

依赖注入
生命周期使用场景
AddSingleton<T>全应用状态、设备服务、设置
AddScoped<T>每个 BlazorWebView 实例的状态
AddTransient<T>无状态实用工具、工厂
// Interface for platform-specific implementation
public interface IDeviceService
{
    string GetDeviceModel();
    Task<bool> HasPermissionAsync(string permission);
}

// Platform implementation registered in MauiProgram.cs
builder.Services.AddSingleton<IDeviceService, DeviceService>();
组件生命周期
@code {
    [Parameter] public string Id { get; set; } = "";

    // Called once when component initializes
    protected override async Task OnInitializedAsync()
    {
        await LoadDataAsync();
    }

    // Called when parameters change (including first render)
    protected override void OnParametersSet()
    {
        // React to parameter changes
    }

    // Called after each render
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            // JS interop safe here
        }
    }
}
平台功能访问
// Check platform and access native features
@inject IDeviceService DeviceService

@if (DeviceInfo.Current.Platform == DevicePlatform.Android)
{
    <AndroidSpecificComponent />
}

@code {
    private async Task AccessCameraAsync()
    {
        var status = await Permissions.CheckStatusAsync<Permissions.Camera>();
        if (status != PermissionStatus.Granted)
        {
            status = await Permissions.RequestAsync<Permissions.Camera>();
        }

        if (status == PermissionStatus.Granted)
        {
            // Use camera
        }
    }
}
根据外部事件更新状态
@implements IDisposable
@inject IDataService DataService

<p>@message</p>

@code {
    private string message = "";

    protected override void OnInitialized()
    {
        DataService.OnDataChanged += HandleDataChanged;
    }

    private async void HandleDataChanged(object? sender, EventArgs e)
    {
        message = "Data updated!";
        await InvokeAsync(StateHasChanged); // Required for external events
    }

    public void Dispose()
    {
        DataService.OnDataChanged -= HandleDataChanged;
    }
}

参考文档

如需详细模式和示例,请参阅:

快速参考

常见服务注册
// Services
builder.Services.AddSingleton<ISettingsService, SettingsService>();
builder.Services.AddSingleton<IConnectivity>(Connectivity.Current);
builder.Services.AddSingleton<IGeolocation>(Geolocation.Default);

// ViewModels (if using MVVM)
builder.Services.AddTransient<MainViewModel>();
builder.Services.AddTransient<SettingsViewModel>();

// Pages with DI
builder.Services.AddTransient<MainPage>();
平台检查
// Runtime platform check
if (DeviceInfo.Current.Platform == DevicePlatform.iOS) { }
if (DeviceInfo.Current.Platform == DevicePlatform.Android) { }
if (DeviceInfo.Current.Platform == DevicePlatform.WinUI) { }
if (DeviceInfo.Current.Platform == DevicePlatform.macOS) { }

// Compile-time platform check
#if ANDROID
    // Android-specific code
#elif IOS
    // iOS-specific code
#elif WINDOWS
    // Windows-specific code
#endif
导航模式
// Blazor navigation (within BlazorWebView)
@inject NavigationManager Navigation
Navigation.NavigateTo("/details/123");

// MAUI navigation (to other pages)
await Navigation.PushAsync(new SettingsPage());
await Shell.Current.GoToAsync("//settings");

评论

支持 Markdown