Xamarin Forms Android WebView works. MAUI Android WebView doesn't work

Tony Pitman 80 Reputation points
2024-03-14T16:47:15.7266667+00:00

I am upgrading an app from Xamarin Forms to MAUI. In my app I have a modified version of the HybridWebView to show a custom website with JS / C# interop. It works fine on Xamarin Forms.

While porting this I found some references to using a Handler instead of a Renderer like was being used in Xamarin Forms. I made those changes.

When I run my app in MAUI it shows some of the html content and pages as it should. It can also render google.com just fine.

When I try to render my custom website, all I get is a white page and I can't figure out why.

If I open that same page on the same Android phone using the Chrome web browse by manually browsing to it, that also renders fine.

While debugging I noticed that the Xamarin forms version of my WebView has the following members populated this way:

User's image

When I debug the MAUI version of the app here are those same members:

User's image

I don't know if that makes any difference, but am asking because the Android Chrome browser renders my page like it should.

Does anyone know whether there would be a difference in the ability to render based on whether the Android WebView has a WebChromeClient or not and why the Xamarin Forms version has one, but the MAUI version does not?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2024-03-15T08:19:56.23+00:00

    Hello,

    Based on your above description, you can create a webviewHandler to enable the DomStorageEnabled to access the local html file like following code.

      Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
                {
    #if ANDROID
                    handler.PlatformView.Settings.JavaScriptEnabled = true;
                    handler.PlatformView.Settings.DomStorageEnabled = true;
                 
    #endif
                });
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments