Blazor tutorial for beginners - part 2
以 Blazor Server App 入門教學為主。
JS Interop: Calling JavaScript from C# | Blazor Tutorial 5
JS Interop : JavaScript Interoperability
@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
程式碼紀錄:IdleWidget.razor、GlobalIdleTimer.js
Creating Forms with Validation | Blazor Tutorial 7
簡單的表單範例
Last updated