Home > Net >  .NET MAUI Blazor: How to change MainPage to a custom razor page
.NET MAUI Blazor: How to change MainPage to a custom razor page

Time:12-30

How do I change the App.xaml MainPage to a custom razor page:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }
}

Thanks in advance!

Best regards H

I have tried this: MainPage = new NavigationPage(new Pages.Login());

But the Pages.Login() is of the wrong type with this exception:

enter image description here

CodePudding user response:

Based on the information you've provided, it seems that your class Pages.Login - while being in a Pages namespace - does not inherit from Microsoft.Maui.Controls.Page, and therefore cannot be used in the place of a page.

Try making Pages.Login a child of Microsoft.Maui.Controls.Page.
Here's an example:

namespace Pages;
using Microsoft.Maui.Controls;

internal class Login : Page {
    // Class contents...
}

CodePudding user response:

I didnt manage to solve it direcly via the class, but I did this in the .razor page:

@page "/"
@inject NavigationManager NavManager

@code {
    protected override void OnInitialized()
    {
        NavManager.NavigateTo("/login");
    }
}
  • Related