From 1d77887faa09c4218599097083e0af9715338b49 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 11 Nov 2021 17:16:18 +0800 Subject: [PATCH] Update `AuthenticationStateProviderOnAuthenticationStateChanged` --- .../Themes/Basic/NavToolbar.razor.cs | 9 +++++-- .../Themes/Basic/LoginDisplay.razor.cs | 25 +++++++------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs index 654eeb1d58..791ac08927 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs @@ -19,6 +19,12 @@ namespace Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic private List ToolbarItemRenders { get; set; } = new List(); protected override async Task OnInitializedAsync() + { + await GetToolbarItemRendersAsync(); + AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProviderOnAuthenticationStateChanged; + } + + private async Task GetToolbarItemRendersAsync() { var toolbar = await ToolbarManager.GetAsync(StandardToolbars.Main); @@ -33,12 +39,11 @@ namespace Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic builder.CloseComponent(); }); } - - AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProviderOnAuthenticationStateChanged; } private async void AuthenticationStateProviderOnAuthenticationStateChanged(Task task) { + await GetToolbarItemRendersAsync(); await InvokeAsync(StateHasChanged); } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs index 285e0a59da..fb1c5266b0 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs @@ -15,35 +15,31 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme.Themes.Basic [Inject] protected IMenuManager MenuManager { get; set; } - [CanBeNull] - protected AuthenticationStateProvider AuthenticationStateProvider; + [Inject] + public AuthenticationStateProvider AuthenticationStateProvider { get; set; } [CanBeNull] protected SignOutSessionStateManager SignOutManager; protected ApplicationMenu Menu { get; set; } - + protected override async Task OnInitializedAsync() { Menu = await MenuManager.GetAsync(StandardMenus.User); Navigation.LocationChanged += OnLocationChanged; - LazyGetService(ref AuthenticationStateProvider); LazyGetService(ref SignOutManager); - if (AuthenticationStateProvider != null) - { - AuthenticationStateProvider.AuthenticationStateChanged += - AuthenticationStateProviderOnAuthenticationStateChanged; - } + AuthenticationStateProvider.AuthenticationStateChanged += + AuthenticationStateProviderOnAuthenticationStateChanged; } protected virtual void OnLocationChanged(object sender, LocationChangedEventArgs e) { InvokeAsync(StateHasChanged); } - + private async void AuthenticationStateProviderOnAuthenticationStateChanged(Task task) { Menu = await MenuManager.GetAsync(StandardMenus.User); @@ -53,13 +49,10 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme.Themes.Basic public void Dispose() { Navigation.LocationChanged -= OnLocationChanged; - if (AuthenticationStateProvider != null) - { - AuthenticationStateProvider.AuthenticationStateChanged -= - AuthenticationStateProviderOnAuthenticationStateChanged; - } + AuthenticationStateProvider.AuthenticationStateChanged -= + AuthenticationStateProviderOnAuthenticationStateChanged; } - + private async Task NavigateToAsync(string uri, string target = null) { if (target == "_blank")