本範例是AntDesign Blazor的入門範例,在學習的同時分享出來,以供新手參考。
開啟VS2022,新建Blazor Web App
,命名AntDesignDemo
雙擊AntDesignDemo
工程檔案,新增AntDesign
,或者使用nuget工具搜尋安裝
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<!--這裡新增AntDesign-->
<PackageReference Include="AntDesign" Version="0.16.2" />
</ItemGroup>
</Project>
Components/App.razor
檔案,新增AntDesign
的css和js<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
<!--這裡新增AntDesign樣式-->
<link rel="stylesheet" href="_content/AntDesign/css/ant-design-blazor.css" />
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="AntDesignDemo.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<HeadOutlet @rendermode="@InteractiveServer" />
</head>
<body>
<Routes @rendermode="@InteractiveServer" />
<!--這裡新增AntDesign指令碼-->
<script src="_content/AntDesign/js/ant-design-blazor.js"></script>
<script src="_framework/blazor.web.js"></script>
</body>
</html>
Components/_Imports.razor
檔案,在最後一行新增名稱空間@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using AntDesignDemo
@using AntDesignDemo.Components
//這裡新增名稱空間
@using AntDesign
Components/Routes.razor
檔案,新增AntContainer
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
</Router>
<!--這裡新增AntDesign容器,不新增Modal、提示等無法彈出-->
<AntContainer />
Program.cs
檔案,註冊AntDesign
using AntDesignDemo.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
//這裡註冊AntDesign
builder.Services.AddAntDesign();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// 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.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();
Components/Pages/Home.razor
檔案,新增AntDesign
的按鈕元件進行測試@page "/"
@*//這裡注入MessageService*@
@inject IMessageService _message;
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<Button Type="primary" OnClick="OnHelloClick">Hello World!</Button>
@code {
//按鈕單擊方法
private void OnHelloClick()
{
//提示資訊
_message.Info("Hello AntDesign Blazor!");
}
}