Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddHostObjectToScript does not work in MAUI app (at least for WINDOWS). #26848

Open
eveselov opened this issue Dec 27, 2024 · 1 comment
Open
Labels

Comments

@eveselov
Copy link

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:

<ScrollView>
    <VerticalStackLayout
        <WebView
            x:Name="HtmlWebView"
            HeightRequest="500"
            WidthRequest="300" />

        <Button
            x:Name="CounterBtn"
            Text="Click me" 
            Clicked="OnCounterClicked"
            HorizontalOptions="Fill" />
    </VerticalStackLayout>
</ScrollView>

MainPage.xaml.cs:

namespace MauiApp1 {
public partial class MainPage : ContentPage {
public MainPage() {
this.InitializeComponent();
}

    private void OnCounterClicked(object sender, EventArgs e) {
        this.HtmlWebView.Source = new HtmlWebViewSource {
            Html = "<html><body><h1>Hello!</h1><button onclick='alert(\"Test\"); window.host.HelloWorld()'>Click Me</button></body></html>"
        };
    }
}

}

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

        return builder.Build();
    }
}

}

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}");
}
}
}

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

  1. Create a new MAUI app
  2. Replace the content of MainPage.xaml, MainPage.xaml.cs, MauiProgram.cs
  3. Add JavaScriptHost.cs under Platforms/Windows
  4. Run the app
  5. Set breakpoint in JavaScriptHost.cs on platformView.CoreWebView2.AddHostObjectToScript("host", new JavaScriptHost());
  6. 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

@eveselov eveselov added the t/bug Something isn't working label Dec 27, 2024
@jfversluis
Copy link
Member

Result: exception thrown from AddHostObjectToScript

What exception? Do you have some details?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants