简体   繁体   English

如何检查 Blazor 服务器应用程序中的 iframe 是否已完成加载内容?

[英]How to check if in a Blazor server app a iframe has finished loading content?

I load different htmlpages inside a Iframe in a blazor server app.我在 blazor 服务器应用程序的 Iframe 中加载不同的 htmlpages。 To change the content of the Iframe I use multiple buttons.要更改 Iframe 的内容,我使用了多个按钮。 On click of a button a corresponding Html page is loaded in the Iframe (one Iframe - diffenrent content - changed via button click).单击按钮后,相应的 Html 页面将加载到 Iframe 中(一个 Iframe - 不同的内容 - 通过单击按钮更改)。

How can I check if content is fully loaded after click?点击后如何检查内容是否已完全加载? Reason for this is, right now if the user is clicking different buttons to fast after one an other the Iframe doesn't show content at all (only the backgroundcolor of the underlying grid and stalles).这样做的原因是,现在如果用户单击不同的按钮以快速一个接一个,Iframe 根本不显示内容(只有底层网格的背景颜色和停滞)。 Can I get a bool value back after Iframe has finished loading? Iframe 完成加载后,我可以取回布尔值吗?

Please I wouln't be into Blazor if I had knowledge from Js.如果我有来自 Js 的知识,我不会进入 Blazor。 I have found posts where js is the solution for this but.我发现 js 是解决这个问题的帖子,但是。 I don't know how to properly implemnt that into my blazor app.我不知道如何在我的 blazor 应用程序中正确实现它。 I can't simply copy that js into the hmtlpage.我不能简单地将那个 js 复制到 hmtlpage 中。 It has to be in the file _Host.cshtml but if I put it there the...document.querySelector('myIframe').addEventListener... sayes null in the developmenttools console.它必须在文件 _Host.cshtml 中,但如果我把它放在那里...document.querySelector('myIframe').addEventListener...sayes null 在开发工具控制台中。 Thank you for help.谢谢你的帮助。 Please if it has to do with js be so kind and try to explain it to me as you would to a nuub (what Iam:).如果它与 js 有关系,请尽量向我解释它,就像你对一个 nuub (我是什么 :) 一样。

 <script type="text/javascript"> document.querySelector('myIframe').addEventListener("load", ev => { console.log('iframe is completely loaded'); }) </script>

Blazor can absolutely receive DOM-events, and you can attach to the events directly in the razor code without the need for JS so you can write your logic using C#. Blazor can absolutely receive DOM-events, and you can attach to the events directly in the razor code without the need for JS so you can write your logic using C#.

The trick is to prefix the event's with @ , like @onload in your example, which then takes an EventCallback / Method as a parameter.诀窍是在事件前面加上@ ,例如您的示例中的@onload ,然后将 EventCallback / Method 作为参数。

<iframe src="https://some.embeddedsite.com" @onload="IFrameLoaded" />

@code {
    private void IFrameLoaded(ProgressEventArgs e)
    {
        Console.WriteLine("IFrame loaded");
    }
}

See the docs on ASP.NET Core Blazor event handling for more.有关更多信息,请参阅ASP.NET Core Blazor 事件处理的文档。


But one might also wonder why the old-fashioned way you are trying now is not working... And to that I don't have an answer, more than that I have also struggled with the same in the past.但是人们可能也想知道为什么您现在尝试的老式方法不起作用……对此我没有答案,而且我过去也曾为此苦苦挣扎。 It seems blazor eat events to support it self and doing so often "manually configured" events gets lost.似乎 blazor 吃事件来支持它自己,这样做经常“手动配置”事件丢失。 I have had less problems with that lately though as I have steered more to the "blazor way" of doing things.我最近在这方面遇到的问题较少,因为我更多地转向了做事的“blazor方式”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM