MAUI Blazor 安装教程

环境准备

确保系统安装

  • visual studio 2022 17.3.0 版本
  • net6

使用 Visual Studio 创建项目

步骤一、创建项目
1.Visual Studio 2022
2. 创建一个新项目
3. 选择 .NET MAUI Blazor 应用 并单击 下一步, 为项目选择一个名称和位置,然后点击 下一步, 创建
步骤二、将 BootstrapBlazor 组件添加到现有项目
1. 通过 nuget.org 源搜索 BootstrapBlazor
右键点击解决方案中的项目并选择 Manage Nuget Packages
dotnet add package BootstrapBlazor --version latest
2. 安装 BootstrapBlazor 包
3. 添加样式表文件
在宿主文件中增加主题样式表
  • ~/wwwroot/index.html
<head>
    ...

    <!-- 需引用 BootstrapBlazor.FontAwesome 包 !-->
    <link href="_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css" rel="stylesheet">

    ...
    <link href="css/app.css" rel="stylesheet">
    <link href="MauiApp1.styles.css" rel="stylesheet">
</head>
4. 添加 Javascript 文件
  • ~/wwwroot/index.html
<body>
    ...
    <!-- 增加代码 !-->
    <script src="_content/BootstrapBlazor/js/bootstrap.blazor.bundle.min.js"></script>
    ...
    <script src="_framework/blazor.webview.js" autostart="false" ></script>
</body>
5. 注册 BootstrapBlazor 服务
  • MauiProgram.cs
MauiProgram.cs
var builder = MauiApp.CreateBuilder();

builder.Services.AddMauiBlazorWebView();

// CodeComment
builder.Services.AddBootstrapBlazor();

return builder.Build();
6. 增加命名空间引用
将以下内容添加到 ~/_Imports.razor 文件中,以便 Razor 文件中能识别组件
@using BootstrapBlazor.Components
7. 增加 BootstrapBlazorRoot 组件到 ~/Main.razor 文件中
<BootstrapBlazorRoot>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <PageTitle>Title</PageTitle>
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
    <NotFound>
            <PageTitle>Not found</PageTitle>
                <LayoutView Layout="@typeof(MainLayout)">
                <p>正在玩命开发中 ...</p>
            </LayoutView>
        </NotFound>
    </Router>
</BootstrapBlazorRoot>
步骤三、页面中使用组件
最后一步是在页面中使用 BootstrapBlazor 组件并在浏览器中运行它。例如:
1. 在页面中增加一个 Button 按钮
<Button Color="Color.Primary" Icon="fa-solid fa-font-awesome" Text="测试" />
2. 在 Visual studio 2022F5 运行应用程序

同时使用 Razor 和 Xaml 的混合模式

1. Pages文件夹中添加一个名为 NativeContent.xaml 的 MAUI ContentView 控件文件
  • NativeContent.xaml
<ContentView
    x:Class="MauiApp1.Pages.NativeContent"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <VerticalStackLayout
        Padding="30,0"
        Spacing="25"
        VerticalOptions="Center">

        <Image
            HeightRequest="200"
            HorizontalOptions="Center"
            SemanticProperties.Description="Cute dot net bot waving hi to you!"
            Source="dotnet_bot.png" />

        <Label
            FontSize="32"
            HorizontalOptions="Center"
            SemanticProperties.HeadingLevel="Level1"
            Text="Hello, World!" />

        <Label
            FontSize="18"
            HorizontalOptions="Center"
            SemanticProperties.Description="Welcome to dot net Multi platform App U I"
            SemanticProperties.HeadingLevel="Level2"
            Text="Welcome to .NET Multi-platform App UI" />

        <Button
            x:Name="CounterBtn"
            Clicked="OnCounterClicked"
            HorizontalOptions="Center"
            SemanticProperties.Hint="Counts the number of times you click"
            Text="Click me" />

    </VerticalStackLayout>
</ContentView>
  • NativeContent.xaml.cs
public partial class NativeContent : ContentView
{
    public NativeContent()
    {
        InitializeComponent();
    }

    int count = 0;

    private void OnCounterClicked(object sender, EventArgs e)
    {
        count++;

        if (count == 1)
            CounterBtn.Text = $"Clicked {count} time";
        else
            CounterBtn.Text = $"Clicked {count} times";
    }
}
2. 修改 MainPage.xaml 文件中的 ContentPageTabbedPage(也可使用 NavigationPage 等其他导航组件)
  • MainPage.xaml
<TabbedPage
    x:Class="MauiApp1.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MauiApp1"
    xmlns:pages="clr-namespace:MauiApp1.Pages"
    BackgroundColor="{DynamicResource PageBackgroundColor}">
    <ContentPage Title="Home">
        <BlazorWebView HostPage="wwwroot/index.html">
            <BlazorWebView.RootComponents>
                <RootComponent ComponentType="{x:Type pages:Index}" Selector="#app" />
            </BlazorWebView.RootComponents>
        </BlazorWebView>
    </ContentPage>
    <ContentPage Title="NativeContent">
        <pages:NativeContent />
    </ContentPage>
    <ContentPage Title="Counter">
        <BlazorWebView HostPage="wwwroot/index.html">
            <BlazorWebView.RootComponents>
                <RootComponent ComponentType="{x:Type pages:Counter}" Selector="#app" />
            </BlazorWebView.RootComponents>
        </BlazorWebView>
    </ContentPage>
    <ContentPage Title="Weather">
        <BlazorWebView HostPage="wwwroot/index.html">
            <BlazorWebView.RootComponents>
                <RootComponent ComponentType="{x:Type pages:FetchData}" Selector="#app" />
            </BlazorWebView.RootComponents>
        </BlazorWebView>
    </ContentPage>
</TabbedPage>
  • MainPage.xaml.cs
public partial class MainPage : TabbedPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}
Visual studio 2022F5 运行应用程序
Themes
Bootstrap
Motronic
Ant Design (完善中)
DevUI (制作中)
LayUI (完善中)
An error has occurred. This application may no longer respond until reloaded. Reload
Seems like the connection with the server has been lost. It can be due to poor or broken network. Please hang on while we're trying to reconnect...
Oh snap! Failed to reconnect with the server. This is typically caused by a longer network outage, or if the server has been taken down. You can try to reconnect, but if that does not work, you need to reload the page.
Oh man! The server rejected the attempt to reconnect. The only option now is to reload the page, but be prepared that it won't work, since this is typically caused by a failure on the server.
Bootstrap Blazor 组件库 更新到 6.6.0

首先感谢您对本套组件的关注,目前本套组件已经拥有超过 120 个组件,本组件是基于 Bootstrap 风格的 Blazor 企业级组件库,提供如布局、导航、表单、数据、通知、图标、语音等几大类通用组件,每一个组件都经过静心设计,具有模块化、响应式和优秀的性能。从更多实际场景出发,满足多种场景的需求,极大的减少开发者时间成本,大大缩短开发周期,大幅提高开发效率,并提供了一套 通用权限管理系统 示例工程。Bootstrap Blazor 产品是由一支专业的全职技术团队进行维护,高效的响应速度,多元化的解决方案,长期提供支持,并提供企业级支持。目前已在多家知名国企内部使用,项目最高在线 1200 人稳定运行。右侧为国内人数最多的中文 Blazor QQ 社区二维码,欢迎扫描加群。

组件更新到 6.6.0 更新日志 [传送门] 如果组件给您带来了方便,请您帮忙给项目点亮 Star github gitee

QQGroup
QQ 795206915