Blazor簡(jiǎn)介和快速入門
不熟悉Blazor的同學(xué)可以先看這篇文章大概了解一下。
全面的ASP.NET Core Blazor簡(jiǎn)介和快速入門
BootstrapBlazor介紹
- 使用文檔:https://www.blazor.zone/introduction
- Gitee項(xiàng)目地址:https://gitee.com/LongbowEnterprise/BootstrapBlazor
BootstrapBlazor是一套基于 Bootstrap 和 Blazor 的企業(yè)級(jí)組件庫(kù),可以認(rèn)為是 Bootstrap 項(xiàng)目的 Blazor 版實(shí)現(xiàn)。基于 Bootstrap 樣式庫(kù)精心打造,并且額外增加了 100 多種常用的組件,為您快速開發(fā)項(xiàng)目帶來(lái)非一般的感覺(喜歡Bootstrap風(fēng)格的同學(xué)推薦使用)。
BootstrapBlazor類庫(kù)安裝
管理Nuget程序包=>搜索BootstrapBlazor
進(jìn)行安裝。
BootstrapBlazor庫(kù)注入容器
在Program.cs
中將 BootstrapBlazor 庫(kù)添加到 ASP.NET Core 項(xiàng)目中的依賴關(guān)系注入容器中。
導(dǎo)入BootstrapBlazor組件庫(kù)命名空間
打開_Imports.razor文件,導(dǎo)入BootstrapBlazor組件庫(kù)命名空間:@using BootstrapBlazor.Components。
Menu 導(dǎo)航菜單設(shè)置
MainLayout.razor
@inherits LayoutComponentBase
<Layout SideWidth="0" IsPage="true" ShowGotoTop="true" ShowCollapseBar="true"
IsFullSide="@IsFullSide" IsFixedHeader="@IsFixedHeader" IsFixedFooter="@IsFixedFooter" ShowFooter="@ShowFooter"
TabDefaultUrl="/"
Menus="@Menus" UseTabSet="@UseTabSet" AdditionalAssemblies="new[] { GetType().Assembly }" class="@Theme">
<Header>
<span class="ms-3 flex-sm-fill d-none d-sm-block">Bootstrap of Blazor</span>
<div class="flex-fill d-sm-none">
</div>
<div class="layout-drawer" @onclick="@(e => IsOpen = !IsOpen)"><i class="fa fa-gears"></i></div>
</Header>
<Side>
<div class="layout-banner">
<div class="layout-title">
<span>EasySQLite</span>
</div>
</div>
<div class="layout-user">
<img class="layout-avatar" src="./favicon.png">
<div class="layout-title">
<span>管理員</span>
</div>
<div class="layout-user-state"></div>
</div>
</Side>
<Main>
<CascadingValue Value="this" IsFixed="true">
@Body
</CascadingValue>
</Main>
<Footer>
<div class="text-center flex-fill">
<a class="page-layout-demo-footer-link" href="https://gitee.com/LongbowEnterprise/BootstrapAdmin" target="_blank">Bootstrap Admin</a>
</div>
</Footer>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFound>
</Layout>
<Drawer Placement="Placement.Right" @bind-IsOpen="@IsOpen" IsBackdrop="true">
<div class="layout-drawer-body">
<div class="btn btn-info w-100" @onclick="@(e => IsOpen = false)">點(diǎn)擊關(guān)閉</div>
<div class="page-layout-demo-option">
<p>布局調(diào)整</p>
<div class="row">
<div class="col-6">
<div class="layout-item @(IsFullSide ? "active d-flex" : "d-flex")" @onclick="@(e => IsFullSide = true)" data-toggle="tooltip" title="左右結(jié)構(gòu)">
<div class="layout-left d-flex flex-column">
<div class="layout-left-header"></div>
<div class="layout-left-body flex-fill"></div>
</div>
<div class="layout-right d-flex flex-column flex-fill">
<div class="layout-right-header"></div>
<div class="layout-right-body flex-fill"></div>
<div class="layout-right-footer"></div>
</div>
</div>
</div>
<div class="col-6">
<div class="layout-item flex-column @(IsFullSide ? "d-flex" : "active d-flex")" @onclick="@(e => IsFullSide = false)" data-toggle="tooltip" title="上下結(jié)構(gòu)">
<div class="layout-top">
</div>
<div class="layout-body d-flex flex-fill">
<div class="layout-left">
</div>
<div class="layout-right flex-fill">
</div>
</div>
<div class="layout-footer">
</div>
</div>
</div>
</div>
</div>
<div class="page-layout-demo-option">
<p>固定調(diào)整</p>
<div class="row">
<div class="col-6 d-flex align-items-center">
<Switch @bind-Value="@IsFixedHeader" OnColor="@Color.Success" OffColor="@Color.Secondary"></Switch>
</div>
<div class="col-6 text-right">
<span class="cell-label">固定頁(yè)頭</span>
</div>
</div>
<div class="row mt-3">
<div class="col-6 d-flex align-items-center">
<Switch @bind-Value="@IsFixedFooter" OnColor="@Color.Success" OffColor="@Color.Secondary"></Switch>
</div>
<div class="col-6 text-right">
<span class="cell-label">固定頁(yè)腳</span>
</div>
</div>
<div class="row mt-3">
<div class="col-6 d-flex align-items-center">
<Switch @bind-Value="@ShowFooter" OnColor="@Color.Success" OffColor="@Color.Primary"></Switch>
</div>
<div class="col-6 text-right">
<span class="cell-label">顯示頁(yè)腳</span>
</div>
</div>
</div>
<div class="page-layout-demo-option">
<p>主題配色</p>
<div class="row">
<div class="col-2">
<span class="color color1" @onclick="@(e => Theme = "color1")"></span>
</div>
<div class="col-2">
<span class="color color2" @onclick="@(e => Theme = "color2")"></span>
</div>
<div class="col-2">
<span class="color color3" @onclick="@(e => Theme = "color3")"></span>
</div>
<div class="col-2">
<span class="color color4" @onclick="@(e => Theme = "color4")"></span>
</div>
<div class="col-2">
<span class="color color5" @onclick="@(e => Theme = "color5")"></span>
</div>
<div class="col-2">
<span class="color color6" @onclick="@(e => Theme = "color6")"></span>
</div>
</div>
</div>
<div class="page-layout-demo-option">
<p>更多設(shè)置</p>
<div class="row">
<div class="col-6 d-flex align-items-center">
<Switch @bind-Value="@UseTabSet" OnColor="@Color.Success" OffColor="@Color.Primary"></Switch>
</div>
<div class="col-6 text-right">
<span class="cell-label">@(UseTabSet ? "多標(biāo)簽" : "單頁(yè)")</span>
</div>
</div>
</div>
</div>
</Drawer>
MainLayout.razor.cs
public partial class MainLayout
{
private bool UseTabSet { get; set; } = true;
private string Theme { get; set; } = "";
private bool IsOpen { get; set; }
private bool IsFixedHeader { get; set; } = true;
private bool IsFixedFooter { get; set; } = true;
private bool IsFullSide { get; set; } = true;
private bool ShowFooter { get; set; } = true;
private List<MenuItem>? Menus { get; set; }
/// <summary>
/// OnInitialized 方法
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
Menus = GetIconSideMenuItems();
}
private static List<MenuItem> GetIconSideMenuItems()
{
var menus = new List<MenuItem>
{
new MenuItem() { Text = "Home", Icon = "fa-solid fa-fw fa-flag", Url = "/" , Match = NavLinkMatch.All},
new MenuItem() { Text = "班級(jí)管理", Icon = "fa-solid fa-fw fas fa-user-secret", Url = "SchoolClass" },
new MenuItem() { Text = "學(xué)生管理", Icon = "fa-solid fa-fw fas fa-universal-access", Url = "Student" },
};
return menus;
}
}
Collapse 折疊面板組件引入
Home.razor
@page "/"
<!-- 引用 BootstrapBlazor.FontAwesome 字體庫(kù)包 -->
<link href="_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css" rel="stylesheet">
<!-- 引用 BootstrapBlazor 組件庫(kù)包 -->
<link href="_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css" rel="stylesheet">
<!--引入BootstrapBlazor 組件庫(kù)包-->
<script src="_content/BootstrapBlazor/js/bootstrap.blazor.bundle.min.js"></script>
<PageTitle>Home</PageTitle>
<h2>七天.NET 8操作SQLite入門到實(shí)戰(zhàn)詳細(xì)教程</h2>
<h5>SQLite是一個(gè)輕量級(jí)的嵌入式關(guān)系型數(shù)據(jù)庫(kù),它以一個(gè)小型的C語(yǔ)言庫(kù)的形式存在。它是一個(gè)自包含、無(wú)需服務(wù)器、零配置的數(shù)據(jù)庫(kù)引擎。與傳統(tǒng)的數(shù)據(jù)庫(kù)系統(tǒng)不同,SQLite直接讀寫普通磁盤文件,不需要單獨(dú)的數(shù)據(jù)庫(kù)服務(wù)器。它支持標(biāo)準(zhǔn)的SQL查詢語(yǔ)言,并提供了事務(wù)支持和ACID屬性(原子性、一致性、隔離性和持久性)。</h5>
<div class="images-item" style="width:60%;margin-top:10px;">
<ImageViewer Url="./七天.NET 8操作SQLite入門到實(shí)戰(zhàn).png" ShowPlaceHolder="false" />
</div>
<DemoBlock Title="基礎(chǔ)用法" Introduction="可同時(shí)展開多個(gè)面板,面板之間不影響" Name="Normal">
<Collapse OnCollapseChanged="@OnChanged">
<CollapseItems>
<CollapseItem Text="嵌入式">
<div>SQLite的庫(kù)可以輕松地嵌入到應(yīng)用程序中,不需要獨(dú)立的數(shù)據(jù)庫(kù)服務(wù)器進(jìn)程。</div>
</CollapseItem>
<CollapseItem Text="無(wú)服務(wù)器" IsCollapsed="false">
<div>與大多數(shù)數(shù)據(jù)庫(kù)系統(tǒng)不同,SQLite不需要單獨(dú)的數(shù)據(jù)庫(kù)服務(wù)器,所有數(shù)據(jù)都存儲(chǔ)在一個(gè)磁盤文件中。</div>
</CollapseItem>
<CollapseItem Text="零配置">
<div>使用SQLite時(shí),沒有任何復(fù)雜的配置或管理任務(wù)。只需引入SQLite庫(kù),并開始使用即可。</div>
</CollapseItem>
<CollapseItem Text="輕量級(jí)">
<div>SQLite是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù)引擎,庫(kù)文件的大小很小,并且在內(nèi)存使用方面也非常高效。</div>
</CollapseItem>
</CollapseItems>
</Collapse>
<ConsoleLogger @ref="NormalLogger" />
</DemoBlock>
<AttributeTable Items="@GetAttributes()" />
Home.razor.cs
public partial class Home
{
[NotNull]
private ConsoleLogger? NormalLogger { get; set; }
private Task OnChanged(CollapseItem item)
{
NormalLogger.Log($"{item.Text}: {item.IsCollapsed}");
return Task.CompletedTask;
}
private bool State { get; set; }
private void OnToggle()
{
State = !State;
}
/// <summary>
/// 獲得屬性方法
/// </summary>
/// <returns></returns>
private AttributeItem[] GetAttributes() =>
[
new()
{
Name = "CollapseItems",
Description = "內(nèi)容",
Type = "RenderFragment",
ValueList = " — ",
DefaultValue = " — "
},
new()
{
Name = "IsAccordion",
Description = "是否手風(fēng)琴效果",
Type = "bool",
ValueList = "true|false",
DefaultValue = "false"
},
new()
{
Name = "OnCollapseChanged",
Description = "項(xiàng)目收起展開狀態(tài)改變回調(diào)方法",
Type = "Func<CollapseItem, Task>",
ValueList = " — ",
DefaultValue = " — "
}
];
}
public class AttributeItem
{
/// <summary>
/// 獲取或設(shè)置屬性的名稱。
/// </summary>
public string Name { get; set; }
/// <summary>
/// 獲取或設(shè)置屬性的描述。
/// </summary>
public string Description { get; set; }
/// <summary>
/// 獲取或設(shè)置屬性的類型。
/// </summary>
public string Type { get; set; }
/// <summary>
/// 獲取或設(shè)置屬性的取值列表(如果有)。
/// </summary>
public string ValueList { get; set; }
/// <summary>
/// 獲取或設(shè)置屬性的默認(rèn)值。
/// </summary>
public string DefaultValue { get; set; }
}
該文章在 2024/8/21 15:02:44 編輯過