提前致谢,
我想为包含在网页中的 java 脚本代码设置一个对象。我在 WPF 窗口中显示此网页,该窗口引用名称为“webView”的 WebView2。
我对 WebView2 很陌生,我不确定如何在 WebView2 SDK 的 AddHostObjectToScript() 方法中传递“名称”和“对象”
这是我尝试过的:
[ComVisible(true)]
public class ObjectHandle
{
public Products(IPriceService priceDetails,IBillService billDetails)
{
PriceDetails = priceDetails;
BillDetails = billDetails;
}
public IPriceService PriceDetails { get; private set; }
public IBillService BillDetails { get; private set; }
}
public class WebViewBrowser:Window
{
public WebViewBrowser()
{
InitializeComponent();
InitializeAsync();
}
private void SetScriptingObject
{
ObjectHandle objHandle = new ObjectHandle(priceDetails,billDetails);
webView.corewebview2.AddHostObjectToScript("ObjectHandle",objHandle );
}
async void InitializeAsync()
{
await webView.EnsureCoreWebView2Async(null);
}
}