diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 569ead45b1f..fd08f5e5aa8 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 9.3.1-beta10 + 9.3.1-beta11 diff --git a/src/BootstrapBlazor/Components/IFrame/IFrame.razor.cs b/src/BootstrapBlazor/Components/IFrame/IFrame.razor.cs index 742f230eba5..93873635207 100644 --- a/src/BootstrapBlazor/Components/IFrame/IFrame.razor.cs +++ b/src/BootstrapBlazor/Components/IFrame/IFrame.razor.cs @@ -28,6 +28,12 @@ public partial class IFrame [Parameter] public Func? OnPostDataAsync { get; set; } + /// + /// 获得/设置 页面加载完毕后回调方法 + /// + [Parameter] + public Func? OnReadyAsync { get; set; } + private string? ClassString => CssBuilder.Default("bb-frame") .AddClassFromAttributes(AdditionalAttributes) .Build(); @@ -53,7 +59,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender) /// /// /// - protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(CallbackAsync)); + protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(TriggerPostData)); /// /// 推送数据方法 @@ -68,11 +74,24 @@ protected override async Task OnAfterRenderAsync(bool firstRender) /// /// [JSInvokable] - public async Task CallbackAsync(object? data) + public async Task TriggerPostData(object? data) { if (OnPostDataAsync != null) { await OnPostDataAsync(data); } } + + /// + /// 由 JavaScript 调用 + /// + /// + [JSInvokable] + public async Task TriggerLoaded() + { + if (OnReadyAsync != null) + { + await OnReadyAsync(); + } + } } diff --git a/src/BootstrapBlazor/Components/IFrame/IFrame.razor.js b/src/BootstrapBlazor/Components/IFrame/IFrame.razor.js index ce9355a0af8..d9c5ffefa9d 100644 --- a/src/BootstrapBlazor/Components/IFrame/IFrame.razor.js +++ b/src/BootstrapBlazor/Components/IFrame/IFrame.razor.js @@ -6,24 +6,19 @@ export function init(id, invoke, callback) { } Data.set(id, handler) - window.addEventListener('message', handler) -} + window.addEventListener('message', handler); + const frame = document.getElementById(id); -export function execute(id, data) { - const frame = document.getElementById(id) - if (frame) { - if (frame.loaded) { - frame.contentWindow.postMessage(data) - } - else { - frame.onload = () => { - frame.loaded = true - frame.contentWindow.postMessage(data) - } - } + frame.onload = () => { + invoke.invokeMethodAsync("TriggerLoaded"); } } +export async function execute(id, data) { + const frame = document.getElementById(id); + frame.contentWindow.postMessage(data); +} + export function dispose(id) { const handler = Data.get(id) Data.remove(id) diff --git a/test/UnitTest/Components/IFrameTest.cs b/test/UnitTest/Components/IFrameTest.cs index 02504d4c6a5..38f4b26451f 100644 --- a/test/UnitTest/Components/IFrameTest.cs +++ b/test/UnitTest/Components/IFrameTest.cs @@ -8,7 +8,7 @@ namespace UnitTest.Components; public class IFrameTest : BootstrapBlazorTestBase { [Fact] - public void Frame_Ok() + public async Task Frame_Ok() { var postData = false; var cut = Context.RenderComponent