maui: Blazor startup not working on Android

Description

Based on the documentation in order to call C# backend from BlazorWebView we must wait for Blazor initialization. On Windows mode it is working as expected. But on Android the following exception is thrown:

[chromium] [INFO:CONSOLE(1)] "Uncaught (in promise) TypeError: window.external.receiveMessage is not a function", source: https://0.0.0.0/_framework/blazor.webview.js (1)
[chromium] [INFO:CONSOLE(1)] "Uncaught (in promise) Error: Blazor has already started.", source: https://0.0.0.0/_framework/blazor.webview.js (1)

Please fix it as the MAUI is not working on Android. So at least I am not able to start the app as in my case a call to backend is required on startup.

Steps to Reproduce

  1. Clone the repo: https://github.com/fairking/MauiApp1.git
  2. Open the project in VS
  3. Run in Windows Mode, you will see the following message: “Hello World OK!”
  4. Now run in Android Mode, you will see the following message: “Loading…” and VS output will log the errors mentioned in the description above.

Version with bug

6.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 9.0

Did you find any workaround?

No 😦

Relevant log output

[chromium] [INFO:CONSOLE(1)] "Uncaught (in promise) TypeError: window.external.receiveMessage is not a function", source: https://0.0.0.0/_framework/blazor.webview.js (1)
[chromium] [INFO:CONSOLE(1)] "Uncaught (in promise) Error: Blazor has already started.", source: https://0.0.0.0/_framework/blazor.webview.js (1)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 32 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you guys for the help and the explanation. Please allow me to look at the docs and do the homework first. Ones I got it running I will close the ticket. I believe I won’t have any more questions. FYI @Eilon @TanayParikh

Thank you guys for all your help and support 👍

Yes, exactly.

@fairking thank you, understood. I’ll take a look and hopefully we can come up with a clean solution to this.

Hi @fairking , can you make sure that the repro project at https://github.com/fairking/MauiApp1.git is up-to-date with the latest code, and then we will investigate? I want to make sure we see whatever you are seeing.

This is happening because Blazor.start() is being called twice.

In your code here as well as from within the MAUI Android Blazor WebKitWebViewClient.

Remove the call in your code to Blazor.start() and wait till window.__BlazorStarted == true before calling into dotnet.

The part about waiting for window.__BlazorStarted == true isn’t optimal. Not sure if @Eilon has a better suggestion?