55
66using Microsoft . Extensions . Options ;
77using Microsoft . JSInterop ;
8+ using System . Globalization ;
89
910namespace BootstrapBlazor . Server . Components . Layout ;
1011
1112/// <summary>
1213/// 母版页基类
1314/// </summary>
14- public partial class BaseLayout : IDisposable
15+ public partial class BaseLayout : IAsyncDisposable
1516{
1617 [ Inject ]
1718 [ NotNull ]
@@ -50,6 +51,8 @@ public partial class BaseLayout : IDisposable
5051 private string ? CancelText { get ; set ; }
5152
5253 private bool _init = false ;
54+ private JSModule ? _module ;
55+ private DotNetObjectReference < BaseLayout > ? _interop ;
5356
5457 /// <summary>
5558 /// <inheritdoc/>
@@ -71,13 +74,18 @@ protected override void OnInitialized()
7174 /// <inheritdoc/>
7275 /// </summary>
7376 /// <returns></returns>
74- protected override async Task OnInitializedAsync ( )
77+ protected override async Task OnAfterRenderAsync ( bool firstRender )
7578 {
76- await base . OnInitializedAsync ( ) ;
79+ await base . OnAfterRenderAsync ( firstRender ) ;
7780
78- var module = await JSRuntime . LoadModule ( $ "{ WebsiteOption . Value . JSModuleRootPath } Layout/BaseLayout.razor.js") ;
79- await module . InvokeVoidAsync ( "initTheme" ) ;
80- _init = true ;
81+ if ( firstRender )
82+ {
83+ _module = await JSRuntime . LoadModule ( $ "{ WebsiteOption . Value . JSModuleRootPath } Layout/BaseLayout.razor.js") ;
84+ _interop = DotNetObjectReference . Create ( this ) ;
85+ await _module . InvokeVoidAsync ( "doTask" , _interop ) ;
86+ _init = true ;
87+ StateHasChanged ( ) ;
88+ }
8189 }
8290
8391 private async Task NotifyCommit ( DispatchEntry < GiteePostBody > payload )
@@ -121,25 +129,55 @@ private async Task NotifyReboot(DispatchEntry<bool> payload)
121129 }
122130 }
123131
132+ /// <summary>
133+ /// 显示投票弹窗
134+ /// </summary>
135+ /// <returns></returns>
136+ [ JSInvokable ]
137+ public async Task ShowVoteToast ( )
138+ {
139+ // 英文环境不投票
140+ if ( CultureInfo . CurrentUICulture . Name == "en-US" )
141+ {
142+ return ;
143+ }
144+
145+ _option = new ToastOption ( )
146+ {
147+ Category = ToastCategory . Information ,
148+ Title = "Gitee 评选活动" ,
149+ IsAutoHide = false ,
150+ ChildContent = RenderVote ,
151+ PreventDuplicates = true
152+ } ;
153+ await Toast . Show ( _option ) ;
154+ }
155+
124156 /// <summary>
125157 /// 释放资源
126158 /// </summary>
127159 /// <param name="disposing"></param>
128- private void Dispose ( bool disposing )
160+ private async ValueTask DisposeAsync ( bool disposing )
129161 {
130162 if ( disposing )
131163 {
132164 CommitDispatchService . UnSubscribe ( NotifyCommit ) ;
133165 RebootDispatchService . UnSubscribe ( NotifyReboot ) ;
166+
167+ if ( _module != null )
168+ {
169+ await _module . InvokeVoidAsync ( "dispose" ) ;
170+ await _module . DisposeAsync ( ) ;
171+ }
134172 }
135173 }
136174
137175 /// <summary>
138176 /// 释放资源
139177 /// </summary>
140- public void Dispose ( )
178+ public async ValueTask DisposeAsync ( )
141179 {
142- Dispose ( true ) ;
180+ await DisposeAsync ( true ) ;
143181 GC . SuppressFinalize ( this ) ;
144182 }
145183}
0 commit comments