From 1760688d2400a8c5d97db87e377e802b5408dcee Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 20 Mar 2018 12:56:22 +0000 Subject: [PATCH] In BlazorComponent, add OnParametersSetAsync and make SetParameters overridable --- .../Components/BlazorComponent.cs | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.AspNetCore.Blazor/Components/BlazorComponent.cs b/src/Microsoft.AspNetCore.Blazor/Components/BlazorComponent.cs index 1f947a25ed..686a8323b0 100644 --- a/src/Microsoft.AspNetCore.Blazor/Components/BlazorComponent.cs +++ b/src/Microsoft.AspNetCore.Blazor/Components/BlazorComponent.cs @@ -77,6 +77,13 @@ namespace Microsoft.AspNetCore.Blazor.Components { } + /// + /// Method invoked when the component has received parameters from its parent in + /// the render tree, and the incoming values have been assigned to properties. + /// + protected virtual Task OnParametersSetAsync() + => null; + /// /// 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) + + /// + /// Method invoked to apply initial or updated parameters to the component. + /// + /// The parameters to apply. + 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)