copy Copy chevron-down
Blazor tutorial for beginners - part 1 以 Blazor Server App 入門教學為主。
為此 Blazor Fundamentals Tutorialarrow-up-right 教學系列影片(於2019年第四季)練習與筆記。
發現另一個 Blazor tutorial for beginnersarrow-up-right 教學系列影片(於2021年第一季)。
這一套也可以 Blazor C# Tutorialsarrow-up-right 教學系列影片(於2020年五月)。
Blazor server-side vs client-side (WebAssembly) | What should you choose?
No need for a server side
No need for JavaScript
Offline support
No need for JavaScript
Small size, fast loading
You are using .NET Core
Debug like a boss
Runs on any browser
Your code stats on the server
Each browser session is an open SignalR connection
Big donwload size
Requires WebAssembly
Less mature runtime
Debugging is not great
Still in preview
Your DLLs are downloaded
You need a server-side
No offline support
Highher latency, worse UX
Hard to maintain and scale
Scaling SignalR in non-Azure fashion is pretty hard
What are Razor Components? | Blazor Tutorial 1
React Component
Each component has its own state.
Components can have Parameters.
How to supply parameters to the component?
Assign them upon reference.
[Parameter] , ChildContent, etc
[CascadingParameter]、CascadingValue
Provide them via Routing.
Razor components support Data Binding
We can bind object to DOM elements and have object changes manipulate the DOM to reflect those changes in the state.
OnInitialized(), OnInitializedAsync()
OnParmetersSet(), OnParametersSetAsync()
在 render 之前,在 initalize 之後。
OnAfterRender(bool firstRender), OnAfterRenderAsync(bool firstRender)
circle-exclamation
關於 firstRender 與 OnInitialize 在應用上的差別
一般的元件狀態值初始化可在 Initialize 執行。若是要存取前端的資源,如:LocalStorage, SessionStorage, Timer的初始化需在此 firstRender 執行。
Components can be disposable
@implements IDisposable
補充:Blazor Server App 啟動順序
以下為 Blazor App 在 .NET5 版本的啟動順序,.NET6 把 Startup.cs 併入 Program.cs 原則上啟動順序是一樣的。
_Host.cshtml
import website resource and assets.
補充:Blazor Component 完整說明
完整的官方文件說明 Blazor Component (官方名稱:Razor Component),為必讀文章。
Blazor Component 完整說明 ASP.NET Core Blazor 6.0 完整說明 Quoting Introduction to Razor Pages in ASP.NET Corearrow-up-right :
Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views.
As you can see on tree structure, a razor page is a cshtmlfile (template) plus acs` file (behavior). The page is rendered as html and send to navigator.
Exists another kind of apps, blazor. Quoting Introduction to ASP.NET Core Blazorarrow-up-right :
Blazor is a framework for building interactive client-side web UI with .NET
Important term "interactive", not only render html, is a language to make page interactive (no just render html on server and send it to client)
請直接參考附件。
ASP.NET Core Razor component lifecycle 請直接參考附件。
Props Down, Events Up 現在依然是前端開發的父子層基本溝通機制。
在Blazor App中 Props Down的實作就是用Parameter傳遞參數到子元件。
Events Up的實作則是 EventCallback<TValue> 的傳遞與向上invoke。
這裡只展示events up 的範例。
共用資源
Data-Binding 範例程式碼
雙向繫結範例
雙向繫結(2-way binding)運作原理除依循「Properties down, events up」原則外還加入一些其他規則。
自訂輸入元件 Data-Binding
Dependency Injection | Blazor Tutorial 2
Dependency Injection (DI)
MVC6 引入DI 機制為基礎框架之一。用一句話說明:讓 service可以隨時隨地 @inject。
DI 物件的生命週期 (※必考題)
Transient (短暫的)
The classs is instantiated once per service resolution.
用於數據交易,如:database transaction。
Singleton (唯一的)
The class is instantiated once for the whole application.
Scoped (有範圍的)
The class is instantiated once per scope.
※ scoped in WebAssembly Blazor works like Singleton.
NavigationManager
used in navigation and routing
一個簡單的 Service 註冊與 @inject 範例
補充:ASP.NET Core - Dependency Injection
關於註冊 service 至少要一門專篇,DI 編程又是另一門專篇,故不在入門期深入討論。@inject 的標的不只是 Razor Component, 也可以是一般的 Class,injection 的插入點可以是 constructor(建構式), method(成員函式), property(成員屬性) 都可以。
What are Blazor Layouts? | Blazor Tutorial 3
Everything starts with the Default Layout
In order to turn a componnet into a Layout you need to:
1) Inhert from LayoutComponentBase
2) Use the @Body parameter
Blazor Server App 啟動執行順序 (※必考題)
Startup.cs
ConfigureServices(),註冊 service
Pages/_Host.cshtml
載入網頁靜態資源,如: MudBlazor, Bootstrap 5 等等
MainLayout 預設程式碼
可在@page指定layout。
Routing and Navigation | Blazor Tutorial 4
Page Routing --- 直接讀碼
routing 詳情請直接看參考資料。
Navigation Support
@inject NavigationManager