Slides made by Quarto & GitHub Action:
Singla Page Application(SPA) frammework for building interactive client-side web UI using .NET Core/5+ C# without “too much” client-side JavaScript.
A self-contained chunk of user interface (UI), such as a page, dialog, or form.
Includes HTML markup and the processing logic required to inject data or respond to UI events.
.razor
file extension:
@page
) or @layout
directive@using
, @implements
, @inherits
, @inject
for C# lang syntax and DI@code
block for C# code@page "/fetchdata"
@using BlazorWasmAppDemo.Shared
@inject HttpClient Http
<PageTitle>Weather forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
}
Tip
A “scoped” CSS file ( .razor.css ) can be added to a component to provide isolated CSS only for it.
Support CSS Variables, so we can programmatically change CSS style in C# code.
Button.razor.css
Button.razor
<button style="--btn-width:@CssAttributes.Width;
--btn-height:@CssAttributes.Height;
--btn-fontSize:@CssAttributes.FontSize;
--btn-foreground:@CssAttributes.ForegroundColor;
--btn-background:@CssAttributes.BackgroundColor;"
@onclick="MouseClick">
@Content
</button>
@code {
[Parameter]
public ButtonStyle CssAttributes { get; set; } = null!;
/* other code */
}
DemoBlazorDynamicCss example project
@page
directive<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@inject
directive to inject services into components.var builder = WebAssemblyHostBuilder.CreateDefault(args);
/* other builder config code*/
builder.Services.AddScoped(sp =>
new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<IPlatformInfo, PlatformInfo>();
builder.Services.AddScoped<BrowserService>();
await builder.Build().RunAsync();
[JSImport]
/ [JSExport]
attribute in Blazor WASM(.NET 7+)We can use Js Interop to co-operate something that Blazor doesn’t support yet, such as WebRTC or WebGL.
The Power of Blazor Hybrid on .NET MAUI is that we can call .NET MAUI API from Blazor @code
block.
By using Razor Class Library(RCL) to share UI components between Web and App.
AdditionalAssemblies
屬性並指定至搬移到 RCL 專案的 MainLayout 元件AdditionalAssemblies
屬性設定,以及配套 asp.net core 後端專案移除掉不使用的 API Controller, Program.cs
修改為正確的 DI 服務設定.Shared
)類別庫(Class Library)專案也刪除app.css 21~23 行
.btn:focus, .btn:active:focus, .btn-link.nav-link:focus, .form-control:focus, .form-check-input:focus {
box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
}
app.css 70~101 行
.loading-progress {
position: relative;
display: block;
width: 8rem;
height: 8rem;
margin: 20vh auto 1rem auto;
}
.loading-progress circle {
fill: none;
stroke: #e0e0e0;
stroke-width: 0.6rem;
transform-origin: 50% 50%;
transform: rotate(-90deg);
}
.loading-progress circle:last-child {
stroke: #1b6ec2;
stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%;
transition: stroke-dasharray 0.05s ease-in-out;
}
.loading-progress-text {
position: absolute;
text-align: center;
font-weight: bold;
inset: calc(20vh + 3.25rem) 0 auto 0.2rem;
}
.loading-progress-text:after {
content: var(--blazor-load-percentage-text, "Loading");
}
在 App, Web 都共用 RCL 專案裡定義的 Razor UI 頁面元件,寫一次就同時討好 Web & App UI 開發。
source: https://github.com/windperson/MWC2023WebRtcDemo
Try to follow the BlazorRtc a Blazor WASM WebRTC demo project, but apply on Blazor Hybrid.
The WebRTC API is accessed via Js Interop
The js code is messy, GitHub Copilot Chat is merrily to help you 🧑💻
{r-stretch}