Maui сhange navigation in BlazorWebView

Copper Contributor

How can I change a page in BlazorWebView using Maui? For example, the page '/' was opened and I need to open '/fetch'.

I found how to return to the previous link via js.

Created a Custom Navigation Manager:

 

public class CustomNavigationManager
{
private static IJSRuntime JSRuntime { get; set; }
public CustomNavigationManager(IJSRuntime jSRuntime)
{
    JSRuntime = jSRuntime;
}

public static async Task Navigation(string url)
{
//Microsoft.Maui.Platform;
if (JSRuntime!= null)
{
await JSRuntime.InvokeVoidAsync("navigation", url);
}
}
}

 

 

Which calls the Js code. Which calls the Js code. Which I placed in wwwroot/index.html

 

<script type="text/javascript">
    window.navigation = (url) => {
    window.location.href = url; // Error: There is no content at fetch.
    //history.back();
    //window.location="https://0.0.0.0/fetch"; //Error: There is no content at fetch.
    }
</script>

 

Registering a service

 

builder.Services.AddTransient<CustomNavigationManager>();

 

And inject in Shared/MainLayout.razor

 

@page "/"
@inject CustomNavigationManager navigation

 

And I use it in maui

 

await CustomNavigationManager.Navigation("/fetch");

 


If I use the js code ```history.back();``` then everything works,

but if I want to redirect to /fetch using

 

window.location.href = url;

 

 

then I get an error: There is no content at fetch.

Fetch.razor page

 

@page "/fetch"
@page "/fetch/{id}"

<h1>Test!</h1>

 

 

1 Reply

Hi @Denis_Polagaev,

Thanks for posting your issue here.

However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang