MAUI uses the Masa blazor component library

MAUI uses the Masa blazor component library

Having a beautiful and beautiful component library can achieve twice the result with half the effort

最后更新 6/21/2022 9:09 PM
沙漠尽头的狼
预计阅读 6 分钟
分类
MAUI Blazor
专题
Blazor component library
标签
.NET Blazor MAUI

上一篇(点击阅读)我们实现了 UI 在 Web 端(Blazor Server/Wasm)和客户端(Windows/macOS/Android/iOS)共享,这篇我加上 Masa Blazor组件库的引用,并把前几个月写的时间戳转换工具加上。

1. pre-knowledge

关于 Masa Blazor 请点击Masa Blazor 官网了解:

MASA Blazor

Provides a standard basic component library based on the interactive capabilities of Material Design and BlazorComponent. Provides preset components for standard scenarios such as layout, pop-up standards, Loading, and global exception handling.

2. References to component libraries

组件库的添加参考Masa 官网,这里写下Dotnet9 后台添加记录:

2.1 UI 共享库的修改-Dotnet9.WebApp

  1. UI 共享库 Dotnet9.WebApp 添加Maas.Blazor包,刚好今天(21 号)Masa 发布了0.5.0-preview.3版本,我们下载使用:
Install-Package Masa.Blazor -Version 0.5.0-preview.3

  1. 封装Maas.Blazor组件引用

添加文件./MasaExtensions/MasaSetup.cs,代码如下:

using Microsoft.Extensions.DependencyInjection;

namespace Dotnet9.WebApp.MasaExtensions;

public static class MasaSetup
{
    public static void AddMasaSetup(this IServiceCollection services)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));

        services.AddMasaBlazor();   // 这句关键代码
    }
}

关键代码只有一行services.AddMasaBlazor();,添加扩展类是为了功能扩展,为了其他项目方便使用...

  1. _Imports.razor 引入Masa.Blazor命名空间
@using Masa.Blazor

就这 3 步对 Dotnet9.WebApp的修改。

2.2 Cross-platform project modifications-Dotnet9.MAUI

  1. 修改MauiProgram.cs文件,添加上面封装的扩展方法AddMasaSetup():
using Dotnet9.WebApp.MasaExtensions;    // 第1处:添加这个命名空间

namespace Dotnet9.MAUI;

public static class MauiProgram
{
    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
        builder.Services.AddMasaSetup();    // 第2外:添加扩展方法引入Masa Blazor

        return builder.Build();
    }
}
  1. 添加Masa.Blazor资源文件

修改wwwwroot/index.html文件,添加以下样式(直接复制 Masa.Blazor Blazor WebAssembly使用的资源文件)

<link href="_content/Masa.Blazor/css/masa-blazor.css" rel="stylesheet" />
<link href="_content/Masa.Blazor/css/masa-extend-blazor.css" rel="stylesheet" />

<link
  href="https://cdn.masastack.com/npm/@mdi/font@5.x/css/materialdesignicons.min.css"
  rel="stylesheet"
/>
<link
  href="https://cdn.masastack.com/npm/materialicons/materialicons.css"
  rel="stylesheet"
/>
<link
  href="https://cdn.masastack.com/npm/fontawesome/v5.0.13/css/all.css"
  rel="stylesheet"
/>

<script src="_content/BlazorComponent/js/blazor-component.js"></script>

2.3 Blazor WebAssembly project modification-Dotnet9.Wasm

  1. 修改Program.cs文件,添加上面封装的扩展方法AddMasaSetup():
using Dotnet9.WebApp;
using Dotnet9.WebApp.MasaExtensions;        // 第1处:添加这个命名空间
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<Main>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddMasaSetup();            // 第2外:添加扩展方法引入Masa Blazor
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
  1. 添加Masa.Blazor资源文件

Dotnet9.MAUI

2.4 Blazor Server project modifications-Dotnet9.Server

  1. 修改Program.cs文件,添加上面封装的扩展方法AddMasaSetup():
using Dotnet9.WebApp.MasaExtensions;        // 第1处:添加这个命名空间

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddMasaSetup();            // 第2外:添加扩展方法引入Masa Blazor

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
  1. 添加Masa.Blazor资源文件

修改Pages/_Layout.cshtml文件,添加以下样式(复制 Masa.Blazor Blazor Server使用的资源文件)

<!-- masa blazor css style -->
<link href="_content/Masa.Blazor/css/masa-blazor.css" rel="stylesheet" />
<link href="_content/Masa.Blazor/css/masa-extend-blazor.css" rel="stylesheet" />

<!--icon file,import need to use-->
<link
href="https://cdn.masastack.com/npm/@("@mdi")/font@5.x/css/materialdesignicons.min.css"
rel="stylesheet">
<link
  href="https://cdn.masastack.com/npm/materialicons/materialicons.css"
  rel="stylesheet"
/>
<link
  href="https://cdn.masastack.com/npm/fontawesome/v5.0.13/css/all.css"
  rel="stylesheet"
/>

<!--js(should lay the end of file)-->
<script src="_content/BlazorComponent/js/blazor-component.js"></script>

注意:MAUI Blazor 和 Blazor WebAssembly 两个项目引入 Masa Blazor 资源文件的代码一样,Blazor Server 和前两者主要区别是materialdesignicons.min.css文件:

这里关于Masa.Blazor的引入就介绍完了,总结下关键三步:

  1. 添加Masa.Blazor NuGet 包:Install-Package Masa.Blazor
  2. Masa.Blazor组件注册使用:services.AddMasaBlazor();
  3. 添加Masa.Blazor资源文件:Wasm 是wwwwroot/index.html, blazor server 是_Layout.cshtml,注意两者添加资源文件的区别。

3. Addition of timestamp function

在做 Blazor Server 版本网站时,有过一次时间戳功能开发的介绍(点击阅读),代码很简单,这里不再细说,不能再水了....

4. summary

Masa.Blazor组件库已经添加,并恢复了时间戳功能,下一步,就是继续搭建网站后台了,使用Masa.Blazor搭建框架喽。

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 1/12/2023

Maui Blazor uses a camera

Since the interface in Maui Blazor is rendered by WebView, it cannot be obtained when using the Android camera again, because the native camera needs to be bound with interface components.

继续阅读
同分类 / 同标签 4/26/2022

Using Masa Blazor in MAUI

Using. NET MAUI, you can develop applications that can run from a single shared code base on Android, iOS, macOS, Windows, and Linux(community support), with one set of codes running on multiple ends.

继续阅读