Blazor tutorial for beginners - part 2

以 Blazor Server App 入門教學為主。

JS Interop: Calling JavaScript from C# | Blazor Tutorial 5

  • IJSRuntime: You can call JS code from C#

  • JSInvokableAttribute:You call call C# code from JS

JS Interop : JavaScript Interoperability

Pages/JSInteropDemo.razor
@page "/jsinterop"
@inject IJSRuntime jsRuntime

<h1>JavaScript Interoperability Demo</h1>

<h4>Show Alert</h4>
<input @bind-value="message" />
<button class="btn btn-success" @onclick="ShowAlert">Show Alert</button>

<h4>Call Prompt</h4>
<p>一進入畫面就 focus 到這裡</p>
<input @ref="inputRef" @bind-value="question" />
<button class="btn btn-warning" @onclick="AskQuestion">Ask question</button>
<div>
    答案是<span style="color:mediumvioletred" id="answerSpan"></span>
</div>

<h4>Foucs Element</h4>
<button class="btn btn-secondary" @onclick="FocusElement">Focus on Call Prompt</button>

@code{

    string message = "我是來自C#的文字訊息。";
    string question = "今天象氣如何?";
    ElementReference inputRef;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender) {
            await FocusElement();
        }
    }

    async Task ShowAlert()
    {
        //string msg = "我是來自C#的文字訊息。";
        await jsRuntime.InvokeVoidAsync("showAlert", message);
    }

    async Task AskQuestion()
    {
        var response = await jsRuntime.InvokeAsync<string>("showPrompt", question);
        await jsRuntime.InvokeVoidAsync("setElementTextById", "answerSpan", response);
    }

    async Task FocusElement()
    {
        await jsRuntime.InvokeVoidAsync("focusElement", inputRef);
    }
}

JS Interop: Calling C# methods from JavaScript | Blazor Tutorial 6

由 JS 為啟始向 C# 呼叫函式(資源)應該是不會有此應用情境的,實務上應該用不到也不建議採用JS call CS code,因為 Blazor 的前端資源並不豐富且程式碼繞好大一圈。

但是,由 C# 為啟始向 JS 呼叫函式(資源)現況是必然的,再由 JS 向 C# 呼叫回覆函式也是常有的應用情境, ,因為前端資訊交換機制常是非同步 event-base 的。

下面範例有

  • IJSRuntime

    • to call global JS function.

  • IJSObjectReference

    • import JavaScript function.

    • used to invoke JS function that imported.

  • DotNetObjectReferenceJSInvokable

    • used to let JS invoke the C# function.

程式碼紀錄:IdleWidget.razor、GlobalIdleTimer.js

目的:idle 時間太長自登出。

透過IJSObjectReferenceIJSRuntime介面,動態載入 JS moudle。 使用ElementReference傳遞 Element 給 JavaScript 更新。 使用 DotNetObjectReference傳遞JSInvokable函式讓JavaScript可以回呼(callback)。

監測使用者是否有在使用滑鼠鍵盤等輸入,並累計idle時間,若達到門檻就觸發訊息以登出系統。

Creating Forms with Validation | Blazor Tutorial 7

詳情請參考附件。

Blazor 提供了幾個表單操作元件系結表單:

  • <EditForm />取代 <form /> 標籤。

  • <DataAnnotationValidator />

  • <ValidationSummary/>

  • <ValidationMessage/>

也提供內建表單 input (Built-in form components)元件:

簡單的表單範例

表單的驗證也支援 DataAnnotations 不過在此就懶的再記錄一次,請謨拜 Google 大神。

Last updated