差分
ページの作成:「 Blazorではバインディング変数の値を更新しても画面が自動更新されないことがある。 そのような場合は明示的に「StateHasChange…」
Blazorではバインディング変数の値を更新しても画面が自動更新されないことがある。
そのような場合は明示的に「StateHasChangedメソッド」を叩いて手動更新するとよい。このメソッドは[[Visual Studio for Mac]]の[[インテリセンス]]では何故か出てこないが、ビルドも通るし、実行してもちゃんと動く。不思議。
== 実装例 ==
Time.cshtml
<source>
<p>@now</p>
<input type="text" bind="@now" />
@using System.Threading
@functions
{
string now { get; set; } = "xyz";
protected override void OnInit()
{
var timer = new Timer(new TimerCallback(_ =>
{
// バインディング変数を更新
now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
// 手動更新させる
this.StateHasChanged();
}), null, 0, 1000);
}
}
</source>
== 関連項目 ==
* [[Visual Studio for MacでBlazorを使う]]
[[category: Visual Studio for Mac]]
[[category: Blazor]]
[[category: WebAssembly]]