Maui webview .Navigated event is not triggered when source is a blazor website

Anders 0 Reputation points
2023-12-01T13:58:15.9533333+00:00

Create a simple MAUI app with support for eg. android.

On mainpage add a webview (NOT BlazorWebView):

    <StackLayout
        VerticalOptions="Fill"
        HorizontalOptions="Fill">
        <WebView
            x:Name="mainWebView"
            VerticalOptions="FillAndExpand"
            HorizontalOptions="FillAndExpand"
            />
    </StackLayout>
    

Now try and catch the event:

        MainWebView.Navigated += async (s, e) =>
        {
            await OnNavigatedAsync(s, e);
        };

These events are never triggered because Blazor is an SPA.
How can this be resolved? I haven't found a reliable way to trigger this event, even with a javascript callback to the MAUI webview.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,665 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,008 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 72,511 Reputation points
    2023-12-04T17:38:11.2466667+00:00

    normally you would define a custom protocol. say "myapp:". then in javascript you would navigate with this protocol: "myapp://action?p1=....". because of the custom protocol, the spa navigation will not pickup. you could use a fake domain (http://myapp/)

    here is a sample (without the custom protocol, just add)

    https://github.com/mahop-net/Maui.HybridWebView

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.