He estado siguiendo la documentación de webview2 en el sitio web oficial de Microsoft, pero encontré un problema que no estoy seguro de cómo solucionar.
He agregado un objeto .NET usando AddHostObjectToScript y funciona siempre que la función no tenga parámetros. Al llamar a la función de objeto que tiene un parámetro en JS, sigo recibiendo un error de "el parámetro es incorrecto".
Así es como llamo a los objetos host en la aplicación angular:
result = await window?.chrome?.webview?.hostObjects.bridge.Func("John");
y esto es de mi aplicación WinUI 3.0:
[ComVisible(true)] public class Bridge { public string Func(string param) { return "Example: " + param; } public string Sample() { return "Example: "; } public BridgeAnotherClass AnotherObject { get; set; } = new BridgeAnotherClass(); // Sample indexed property. [System.Runtime.CompilerServices.IndexerName("Items")] public string this[int index] { get { return m_dictionary[index]; } set { m_dictionary[index] = value; } } private Dictionary<int, string> m_dictionary = new Dictionary<int, string>(); } public sealed partial class WebViewPage : Page { public WebViewViewModel ViewModel { get; } public WebViewPage() { ViewModel = Ioc.Default.GetService<WebViewViewModel>(); InitializeComponent(); ViewModel.WebViewService.Initialize(webView); webView.WebMessageReceived += getMsg; InitializeAsync(); } async void InitializeAsync() { await webView.EnsureCoreWebView2Async(); var interop = webView.CoreWebView2.As<ICoreWebView2Interop>(); interop.AddHostObjectToScript("bridge", new Bridge()); }Actualmente, WebView2 tiene un problema en el que la interfaz de interoperabilidad AddHostObjectToScript de la API de WinRT no funciona bien con objetos .NET. Este es un error en WebView2.