Search code examples
maui

How can I change the base class for a .NET MAUI ContentPage?


Assume I have some common utilities for all of my pages so I want to abstract out a base class for all my content pages to derive from. The problem is that a newly created content page is naturally defined as partial and I can't simply change the code-behind part to use the new base class. The compiler will complain: Partial declarations of 'NewPage1' must not specify different base classes CS0263.


Solution

  • As the error suggest, your NewPage1 can't specify two different base classes . You are specifying BasePage in NewPage1.xaml.cs and by default NewPage1.xaml have ContentPage page specified as base class.

    You should set the same base class in xaml also

    <local:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="MyProject.Views.NewPage1"
                 xmlns:local="clr-namespace:MyProject"
                 >
        <VerticalStackLayout>
            <Label 
                Text="Welcome to .NET MAUI!"
                VerticalOptions="Center" 
                HorizontalOptions="Center" />
        </VerticalStackLayout>
    </local:BasePage>