ASP.NET Core Razor 元件生命週期事件, OnInitialized , OnInitializedAsync, OnParametersSet, OnParametersSetAsync, OnAfterRender(bool firstRender), OnAfterRenderAsync(bool firstRender), ShouldRender()
Blazor 元件觸發時的生命週期,一般在正常啟用下會有觸發的事件順序,但在同元件不同屬性載入時有幾個生命週期會減少些事件的觸發,下列紀錄一下在生命中週期的起應用順。
我的元件名稱為 BatchPredictCalculator
#region 元件運行生命週期
/// <summary>
/// 該Blazor中的 [paramete] 屬性變數變更時觸發
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public override async Task SetParametersAsync(ParameterView parameters)
{
await base.SetParametersAsync(parameters);
}
/// <summary>
/// 0. 初始化 - 第一次開啟時執行
/// </summary>
protected override void OnInitialized()
{
}
/// <summary>
/// 0. 初始化非同步 - 第一次開啟時執行
/// </summary>
/// <returns></returns>
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
}
/// <summary>
/// 1. 元件切換時
/// </summary>
protected override void OnParametersSet()
{
base.OnParametersSet();
}
/// <summary>
/// 1. 元件切換時非同步
/// </summary>
/// <returns></returns>
protected async override Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
}
/// <summary>
/// 2. 準備開始渲染元件
/// </summary>
/// <returns></returns>
protected override bool ShouldRender()
{
return base.ShouldRender();
}
/// <summary>
/// 3.元件刷新完畢
/// </summary>
/// <param name="firstRender"></param>
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
}
base.OnAfterRender(firstRender);
}
/// <summary>
/// 3.元件刷新完畢非同步
/// </summary>
/// <param name="firstRender"></param>
/// <returns></returns>
protected async override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
}
await base.OnAfterRenderAsync(firstRender);
}
/// <summary>
/// 元件切換關閉
/// </summary>
public void Dispose()
{
}
/// <summary>
/// 元件切換關閉 非同步
/// </summary>
/// <returns></returns>
public async ValueTask DisposeAsync()
{
if (_module != null)
{
await _module.DisposeAsync();
}
}
/// <summary>
/// 系統服務中的屬性變動時
/// </summary>
/// <param name="o"></param>
/// <param name="e"></param>
private async void PropertyChanged(Object o, System.ComponentModel.PropertyChangedEventArgs e)
{
await InvokeAsync(StateHasChanged);
}
#endregion
留言
張貼留言