Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have easy way to set initial BlazorWebView URL #11746

Closed
Eilon opened this issue Nov 30, 2022 · 1 comment · Fixed by #11942
Closed

Have easy way to set initial BlazorWebView URL #11746

Eilon opened this issue Nov 30, 2022 · 1 comment · Fixed by #11942
Assignees
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! t/enhancement ☀️ New feature or request
Milestone

Comments

@Eilon
Copy link
Member

Eilon commented Nov 30, 2022

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):

  1. Change the default Main.razor file to accept an optional StartPath property, and call NavigationManager.NavigateTo if 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();
        }
    }
  2. In MainPage.xaml.cs set 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; }
}
@Eilon Eilon added t/enhancement ☀️ New feature or request area-blazor Blazor Hybrid / Desktop, BlazorWebView labels Nov 30, 2022
@mkArtakMSFT mkArtakMSFT added this to the 8.0-preview1 milestone Nov 30, 2022
@ismasanchez
Copy link

ismasanchez commented Dec 17, 2022

There's a "hidden bug" with this proposal in relation to #12171 .
Once NavigateTo is fired, if you navigate to another page and press the back button twice on android, router will try to find the root page and will find nothing, so you will get the route not found component:

	<NotFound>
		<LayoutView Layout="@typeof(MainLayoutUtils)">
			<p role="alert">Sorry, there's nothing at this address.</p>
		</LayoutView>
	</NotFound>

This is one of the reasons why #12171 is important among other potential use cases.

@ghost ghost locked as resolved and limited conversation to collaborators Jan 18, 2023
@samhouts samhouts added the fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! label Feb 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! t/enhancement ☀️ New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants