R-2023-11-15 NET 8 正式發佈
來源
重點記錄
- Visual Studio 2022 需升上 17.8
Blazor專案種類
- Server / WebAssembly Standalone : 跟舊版一致。
- Server / WebAssembly Empty : 跟舊版一致,只是內容全空。
- Blazor Web App : 新架構,專案就是類似以前的MVC/Razor Page的ServerSide Render(不是SignalR)。
- 可以兼容Blazor Server or WebAssembly
Blazor Web App重點
專案類型,有3種互動模式及2種互動位置可以挑選:
- Interactive Render Mode: 載入後的行為互動
- Server: SignalR Server互動。
- Web Assembly: 前端互動。
- Auto: 第一次進入由Server SignalR下載,背景會下載Web Assembly dll,之後由前端互動。
- Interactive Render Location: 將上述的Redner Mode 指定為何種設定
- Global: 全域設定,代表所有的Blazor都是同一種Render Mode
- Component: 每一個Blazor/Component自己指定Render Mode,沒指定的話,預設是Razor Page,要互動會是同傳Form Submit Post到後端。
- 專案檔案差異
- Server : 一個Web專案
- Global: App.razor 的Routes/HeadOutlet 會指定rendermode=InteractiveServer。
- Component : Counter.razor 上方有設定render mode、Weather.razor 示範Server Render的Streaming
- Web Assembly: 切分2個專案,Web專案及Client(WASM)專案。
- Global: App.razor 的Routes/HeadOutlet 會指定rendermode=InteractiveWebAssembly。
- 所有Blazor放在Client專案
- 任一網頁瀏覽後,要先下載全部所需的Web Assembly dll
- 這跟NET 7的版本很像,差別在Web專案獨立Blazor Server Render
- Component : Counter.razor 上方有設定render mode、Weather.razor 示範Server Render的Streaming
- 只有指定InteractiveWebAssembly才會放在Client專案。
- 點到rendermode=InteractiveWebAssembly,才會背景下載全部所需的Web Assembly dll
- 這個比較彈性,可以為Page元件,個別指定要哪個模式
- https://www.perplexity.ai/search/blazor-8you-yi-ge-rendermodewe-.0obqQiTTlG3JWteqyO7gg
- Global: App.razor 的Routes/HeadOutlet 會指定rendermode=InteractiveWebAssembly。
- Auto: 同WebAssembly,差別在於rendermode=InteractiveAuto。
- Global: 任一網頁瀏覽後,背景下載全部所需的Web Assembly dll
- Component: 點到任一支rendermode=InteractiveWebAssembly,才會背景下載全部所需的Web Assembly dll
- 所有Blazor (不是單一)
- Server : 一個Web專案
- 結論: 未來應該會以Auto為主,小專案測試才用Server。
- Per Components/Page 預設的渲染模式是 InteractiveAuto
- BlazorApp 目前不支援Router Lazy Load Assembly的方式。
- WebAssebembly的方式會與前端Script較相容,在Server Mode下會有一些意想不到的錯誤。
- 補充: 在Auto模式,在Client 註冊的Inject Service,在Server也要再註冊一次,因為是先Server 後WASM.
其他
- 在專案內pull request 或關聯工作項目
- Replace 大小寫
Csharp 12
- 陣列表達式
int[] x1 = [1, 2, 3, 4];
int[] x2 = [];
WriteByteArray([1, 2, 3]);
List<int> x4 = [1, 2, 3, 4];
Span<DateTime> dates = [GetDate(0), GetDate(1)];
WriteByteSpan([1, 2, 3]);
- 類別建構式
public class BankAccount(string accountID, string owner)
{
public string AccountID { get; } = accountID;
public string Owner { get; } = owner;
public override string ToString() => $"Account ID: {AccountID}, Owner: {Owner}";
}
- 型別別名
using intArray = int[]; // Array types.
using Point = (int x, int y); // Tuple type
- 預設Lamba參數值
var IncrementBy = (int source, int increment = 1) => source + increment;
Console.WriteLine(IncrementBy(5)); // 6
Console.WriteLine(IncrementBy(5, 2)); // 7
其他/參考
- ASP.NET Core Blazor 轉譯模式
- Announcing C# 12
- 已試著照著Blazor App Server 8,將7的升級成功。