Here's a followup to this question.
I'm porting a WPF app from CEFSharp to WebView2. I have a HostObject that needs to be accessible from js in the WebView2 window. This is it, stripped down.
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
{
}
}
}
I can use this line successfully in the WebView2 control's NavigationStarting event to make the object visible from Javascript. So far so good.
webView.CoreWebView2.AddHostObjectToScript("api", new API());
I can retrieve public properties and members like this. So far so good.
(async function foo () {
const api = chrome.webview.hostObjects.api
const ver = await api.Version
alert (ver)
})();
My problem: Can I reliably, without any sort of asynchronous race-condition or deadlock risk, set properties like this? api.Username = 'whoever'
It seems to work but I haven't found it documented.
(async function foo () {
const api = chrome.webview.hostObjects.api
api.Username = 'whoever'
const user = await api.Username
alert (user)
})();
The documentation says the HostObject is exposed through Promises. Am I hitting the setter correctly?