@@ -83,6 +83,13 @@ public partial class Modal
8383 [ Parameter ]
8484 public Func < Task > ? OnCloseAsync { get ; set ; }
8585
86+ /// <summary>
87+ /// <para lang="zh">关闭之前回调方法 返回 true 时关闭弹窗 返回 false 时阻止关闭弹窗</para>
88+ /// <para lang="en">Callback Method Before Closing. Return true to close, false to prevent closing</para>
89+ /// </summary>
90+ [ Parameter ]
91+ public Func < Task < bool > > ? OnClosingAsync { get ; set ; }
92+
8693 /// <summary>
8794 /// <para lang="zh">获得后台关闭弹出窗口的设置</para>
8895 /// <para lang="en">Gets the background close popup setting</para>
@@ -190,6 +197,21 @@ public async Task CloseCallback()
190197 }
191198 }
192199
200+ /// <summary>
201+ /// <para lang="zh">弹出窗口关闭前回调方法,由 JSInvoke 调用</para>
202+ /// <para lang="en">Callback method when the popup before close, called by JSInvoke</para>
203+ /// </summary>
204+ [ JSInvokable ]
205+ public async Task < bool > BeforeCloseCallback ( )
206+ {
207+ var result = true ;
208+ if ( OnClosingAsync != null )
209+ {
210+ result = await OnClosingAsync ( ) ;
211+ }
212+ return result ;
213+ }
214+
193215 /// <summary>
194216 /// <para lang="zh">切换弹出窗口状态的方法</para>
195217 /// <para lang="en">Method to toggle the popup state</para>
@@ -214,7 +236,14 @@ public async Task Show()
214236 /// <para lang="zh">关闭当前弹出窗口的方法</para>
215237 /// <para lang="en">Method to close the current popup</para>
216238 /// </summary>
217- public Task Close ( ) => InvokeVoidAsync ( "execute" , Id , "hide" ) ;
239+ public async Task Close ( )
240+ {
241+ var result = await BeforeCloseCallback ( ) ;
242+ if ( result )
243+ {
244+ await InvokeVoidAsync ( "execute" , Id , "hide" ) ;
245+ }
246+ }
218247
219248 /// <summary>
220249 /// <para lang="zh">设置标题文本的方法</para>
@@ -247,4 +276,33 @@ public void UnRegisterShownCallback(IComponent component)
247276 {
248277 _shownCallbackCache . TryRemove ( component , out _ ) ;
249278 }
279+
280+ /// <summary>
281+ /// <para lang="zh">注册弹出窗口关闭前调用的回调方法,允许自定义逻辑来决定是否继续关闭操作</para>
282+ /// <para lang="en">Registers a callback that is invoked asynchronously when a closing event is triggered, allowing custom logic to determine whether the closing operation should proceed.</para>
283+ /// </summary>
284+ /// <param name="onClosingCallback">
285+ /// <para lang="zh">返回包含布尔值的任务的函数。当关闭事件发生时执行该回调,返回 <see langword="true"/> 允许继续关闭操作,返回 <see langword="false"/> 取消关闭操作</para>
286+ /// <para lang="en">A function that returns a task containing a Boolean value. The callback is executed when the closing event
287+ /// occurs, and should return <see langword="true"/> to allow the closing operation to continue, or <see
288+ /// langword="false"/> to cancel it.</para>
289+ /// </param>
290+ public void RegisterOnClosingCallback ( Func < Task < bool > > onClosingCallback )
291+ {
292+ OnClosingAsync += onClosingCallback ;
293+ }
294+
295+ /// <summary>
296+ /// <para lang="zh">注销弹出窗口关闭前调用的回调方法</para>
297+ /// <para lang="en">Unregisters a previously registered callback that is invoked when a closing event occurs.</para>
298+ /// </summary>
299+ /// <param name="onClosingCallback">
300+ /// <para lang="zh">要从关闭事件中移除的回调函数。该函数应返回一个布尔值的任务,指示是否继续关闭操作</para>
301+ /// <para lang="en">The callback function to remove from the closing event. The function should return a task that evaluates to a
302+ /// Boolean value indicating whether the closing operation should proceed.</para>
303+ /// </param>
304+ public void UnRegisterOnClosingCallback ( Func < Task < bool > > onClosingCallback )
305+ {
306+ OnClosingAsync -= onClosingCallback ;
307+ }
250308}
0 commit comments