In BlazorComponent, add OnParametersSetAsync and make SetParameters overridable
This commit is contained in:
parent
5bf0b891e9
commit
1760688d24
|
|
@ -77,6 +77,13 @@ namespace Microsoft.AspNetCore.Blazor.Components
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Method invoked when the component has received parameters from its parent in
|
||||||
|
/// the render tree, and the incoming values have been assigned to properties.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual Task OnParametersSetAsync()
|
||||||
|
=> null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Notifies the component that its state has changed. When applicable, this will
|
/// Notifies the component that its state has changed. When applicable, this will
|
||||||
/// cause the component to be re-rendered.
|
/// cause the component to be re-rendered.
|
||||||
|
|
@ -115,7 +122,11 @@ namespace Microsoft.AspNetCore.Blazor.Components
|
||||||
_renderHandle = renderHandle;
|
_renderHandle = renderHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IComponent.SetParameters(ParameterCollection parameters)
|
/// <summary>
|
||||||
|
/// Method invoked to apply initial or updated parameters to the component.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameters">The parameters to apply.</param>
|
||||||
|
public virtual void SetParameters(ParameterCollection parameters)
|
||||||
{
|
{
|
||||||
parameters.AssignToProperties(this);
|
parameters.AssignToProperties(this);
|
||||||
|
|
||||||
|
|
@ -126,24 +137,27 @@ namespace Microsoft.AspNetCore.Blazor.Components
|
||||||
|
|
||||||
// If you override OnInitAsync and return a nonnull task, then by default
|
// If you override OnInitAsync and return a nonnull task, then by default
|
||||||
// we automatically re-render once that task completes.
|
// we automatically re-render once that task completes.
|
||||||
OnInitAsync()?.ContinueWith(task =>
|
OnInitAsync()?.ContinueWith(ContinueAfterLifecycleTask);
|
||||||
{
|
|
||||||
if (task.Exception == null)
|
|
||||||
{
|
|
||||||
StateHasChanged();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HandleException(task.Exception);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
OnParametersSet();
|
OnParametersSet();
|
||||||
|
OnParametersSetAsync()?.ContinueWith(ContinueAfterLifecycleTask);
|
||||||
|
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ContinueAfterLifecycleTask(Task task)
|
||||||
|
{
|
||||||
|
if (task.Exception == null)
|
||||||
|
{
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
HandleException(task.Exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void HandleException(Exception ex)
|
private void HandleException(Exception ex)
|
||||||
{
|
{
|
||||||
if (ex is AggregateException && ex.InnerException != null)
|
if (ex is AggregateException && ex.InnerException != null)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue