-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Background
When BlazorWebView initializes Blazor itself, the first thing it does is navigate to / (examples: MAUI Windows, MAUI Android, WinForms).
If you want the initial URL to be something different, you have to do it entirely manually, which in my view is not so obvious.
It's useful to have a custom start page when your app is built around one main Razor component that represents the app, but you want to take the user straight to a specific page within the Blazor navigation space. This could be due to deep-linking from an external location, multi-window apps (e.g. pop open a new window to show a specific order's details, etc.).
Workaround
Here's how to do it manually in MAUI (very similar on WinForms/WPF as well):
- Change the default
Main.razorfile to accept an optionalStartPathproperty, and callNavigationManager.NavigateToif it is set:@inject NavigationManager NavigationManager <!-- normal router stuff here --> <Router ...> </Router> @code { [Parameter] public string StartPath { get; set; } protected override void OnParametersSet() { if (!string.IsNullOrEmpty(StartPath)) { NavigationManager.NavigateTo(StartPath); } base.OnParametersSet(); } }
- In
MainPage.xaml.csset the root component's parameters to indicate which, if any, custom start path to use:public MainPage() { InitializeComponent(); blazorWebView.RootComponents[0].Parameters = new Dictionary<string, object> { { "StartPath", "/counter" }, // hard-code this, or get its value dynamically from somewhere }; }
Proposal
This could possibly be done more easily if each BlazorWebView control had a new StartPath property that would be used for the initial navigation (instead of always navigating to /):
public class BlazorWebView : ...
{
public string? StartPath { get; set; }
}