In BlazorComponent, add OnParametersSetAsync and make SetParameters overridable

This commit is contained in:
Steve Sanderson 2018-03-20 12:56:22 +00:00
parent 5bf0b891e9
commit 1760688d24
1 changed files with 28 additions and 14 deletions

View File

@ -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>
/// Notifies the component that its state has changed. When applicable, this will
/// cause the component to be re-rendered.
@ -114,8 +121,12 @@ namespace Microsoft.AspNetCore.Blazor.Components
_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);
@ -126,24 +137,27 @@ namespace Microsoft.AspNetCore.Blazor.Components
// If you override OnInitAsync and return a nonnull task, then by default
// we automatically re-render once that task completes.
OnInitAsync()?.ContinueWith(task =>
{
if (task.Exception == null)
{
StateHasChanged();
}
else
{
HandleException(task.Exception);
}
});
OnInitAsync()?.ContinueWith(ContinueAfterLifecycleTask);
}
OnParametersSet();
OnParametersSetAsync()?.ContinueWith(ContinueAfterLifecycleTask);
StateHasChanged();
}
private void ContinueAfterLifecycleTask(Task task)
{
if (task.Exception == null)
{
StateHasChanged();
}
else
{
HandleException(task.Exception);
}
}
private void HandleException(Exception ex)
{
if (ex is AggregateException && ex.InnerException != null)