【发布时间】:2021-10-07 21:44:30
【问题描述】:
这是this question 的后续内容。
我正在将 WPF 应用程序从 CEFSharp 移植到 WebView2。我有一个 HostObject 需要从 WebView2 窗口中的 js 访问。就是这样,被剥离了。
using System;
using System.Runtime.InteropServices;
namespace webview2Demo
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Api
{
public string Username { get; set; }
public string Version = "1.1.1";
public Api() //ctor
{
}
}
}
我可以在 WebView2 控件的 NavigationStarting 事件中成功使用这一行来使对象在 Javascript 中可见。到目前为止一切顺利。
webView.CoreWebView2.AddHostObjectToScript("api", new API());
我可以像这样检索公共属性和成员。到目前为止一切顺利。
(async function foo () {
const api = chrome.webview.hostObjects.api
const ver = await api.Version
alert (ver)
})();
我的问题:我能否在没有任何异步竞争条件或死锁风险的情况下可靠地设置这样的属性? api.Username = 'whoever' 它似乎有效,但我没有发现它记录在案。
(async function foo () {
const api = chrome.webview.hostObjects.api
api.Username = 'whoever'
const user = await api.Username
alert (user)
})();
文档说 HostObject 是通过 Promises 公开的。 我击中二传手是否正确?
【问题讨论】:
标签: javascript c# .net wpf webview2