You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am struggling with adding HostObject to jscript for WebView in MAUI app (for WINDOWS). I created a simplest MAUI app from scratch to avoid any special aspects of my own code, It's now the simplest possible:
public class JavaScriptHost {
public JavaScriptHost() {
Debug.WriteLine("JavaScriptHost created");
}
public void HelloWorld() {
Debug.WriteLine("Call from jscript received");
}
}
}
Whatever I am trying to do with this, I am having the same SystemError error in this call:
platformView.CoreWebView2.AddHostObjectToScript("host", new JavaScriptHost());
Steps to Reproduce
Create a new MAUI app
Replace the content of MainPage.xaml, MainPage.xaml.cs, MauiProgram.cs
Add JavaScriptHost.cs under Platforms/Windows
Run the app
Set breakpoint in JavaScriptHost.cs on platformView.CoreWebView2.AddHostObjectToScript("host", new JavaScriptHost());
Click "Click Me" button
Result: exception thrown from AddHostObjectToScript
Link to public reproduction project repository
No response
Version with bug
9.0.21 SR2.1
Is this a regression from previous behavior?
No, this is something new
Last version that worked well
No response
Affected platforms
Windows
Affected platform versions
WINDOWS
Did you find any workaround?
I have to use "post" from jscript to pass information from WebView to C#
Relevant log output
The text was updated successfully, but these errors were encountered:
Description
I am struggling with adding HostObject to jscript for WebView in MAUI app (for WINDOWS). I created a simplest MAUI app from scratch to avoid any special aspects of my own code, It's now the simplest possible:
MainPage.xaml:
MainPage.xaml.cs:
namespace MauiApp1 {
public partial class MainPage : ContentPage {
public MainPage() {
this.InitializeComponent();
}
}
MauiProgram.cs:
namespace MauiApp1 {
public static class MauiProgram {
public static MauiApp CreateMauiApp() {
var builder = MauiApp.CreateBuilder();
builder = builder
.UseMauiApp();
#if WINDOWS
builder = builder.ConfigureMauiHandlers(handlers => {
handlers.AddHandler(typeof(WebView), typeof(MauiApp1.Platforms.Windows.CustomWebViewHandler));
});
#endif
}
JavaScriptHost.cs (in Platforms/Windows):
using Microsoft.Maui.Handlers;
using Microsoft.UI.Xaml.Controls;
using System.Diagnostics;
namespace MauiApp1.Platforms.Windows {
public class CustomWebViewHandler : WebViewHandler {
protected override async void ConnectHandler(WebView2 platformView) {
base.ConnectHandler(platformView);
try {
if (platformView.CoreWebView2 == null) {
await platformView.EnsureCoreWebView2Async();
if (platformView.CoreWebView2 == null) {
Debug.WriteLine("WebView2 initialization failed");
return;
}
}
platformView.CoreWebView2.AddHostObjectToScript("host", new JavaScriptHost());
Debug.WriteLine("WebView2 initialized");
} catch (Exception ex) {
Debug.WriteLine($"WebView2 initialization failed: {ex}");
}
}
}
}
Whatever I am trying to do with this, I am having the same SystemError error in this call:
platformView.CoreWebView2.AddHostObjectToScript("host", new JavaScriptHost());
Steps to Reproduce
Result: exception thrown from AddHostObjectToScript
Link to public reproduction project repository
No response
Version with bug
9.0.21 SR2.1
Is this a regression from previous behavior?
No, this is something new
Last version that worked well
No response
Affected platforms
Windows
Affected platform versions
WINDOWS
Did you find any workaround?
I have to use "post" from jscript to pass information from WebView to C#
Relevant log output
The text was updated successfully, but these errors were encountered: