WebView2-如何获得与WebBrowser相同的功能.文件OpenNew(true)



在System Forms Webbrowser中,以下是每次用新的HTML内容更新Webbrowser时打开新HTML文档的代码

webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(true).Write(html);

在微软。网状物WebView2.WinForms.WebView2我可以使用导航到下面这样的字符串来更新HTML字符串。

webBrowser.NavigateToString(html);

但这不会打开新文档。它在相同的时间更新。WebView2中是否有一种方法可以在每次内容更改时在新文档上设置HTML内容?

有一个javascriptdocument.open(type, replace)可以帮助您实现与旧WebBrowser控件的Document相同的功能。OpenNew(replaceInHistory(。

要将其与WebView2一起使用,您需要调用ExecuteScriptAsync,例如:

await webView21.EnsureCoreWebView2Async();
await webView21.ExecuteScriptAsync(
"document.open('text/html', true);" +
"document.write('<html><body>XXXXXXXXXX</body></html>');");

它将打开一个新文档并替换历史记录。

最新更新