Search code examples
c#.netxamlmaui

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


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


Solution

  • 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...
    }