让 MAUI 页面继承自定义基础 class

Let MAUI page inherit form custom base class

我希望我的页面 class 继承自以下基础 class:

public abstract class BaseContentPage<T> : ContentPage where T : BaseViewModel
{
    public BaseContentPage(T viewModel, string pageTitle)
    {
        BindingContext = ViewModel = viewModel;
        Title = pageTitle;
    }

    protected T ViewModel { get; }
}

public partial class MainPage : BaseContentPage<MainVm>
{
    public MainPage(MainVm vm) : base(vm, "Hello")
    {
        InitializeComponent();
    }
}

页面 class 是部分的,我想,MAUI 生成了一些具有不同父级的隐藏代码 class。然后我得到以下错误:

CS0263: Partial declarations of 'type' must not specify different base classes

有没有办法指定生成的部分 class 的父 class?

编辑

    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:m="clr-namespace:MyProject.Models"
        xmlns:vm="clr-namespace:MyProject.ViewModels"
        x:Class="MyProject.Pages.MainPage"
        xmlns:local="clr-namespace:MyProject">
        <BaseContentPage.Content>
            <StackLayout>
                <Label Text="Welcome to MyProject!"
                    VerticalOptions="CenterAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
            </StackLayout>
        </BaseContentPage.Content>
    </ContentPage>

    <BaseContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:m="clr-namespace:MyProject.Models"
        xmlns:vm="clr-namespace:MyProject.ViewModels"
        x:Class="MyProject.Pages.MainPage"
        x:TypeArguments="vm:MainVm"
        xmlns:local="clr-namespace:MyProject">
        <BaseContentPage.Content>
            <StackLayout>
                <Label Text="Welcome to MyProject!"
                    VerticalOptions="CenterAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
            </StackLayout>
        </BaseContentPage.Content>
    </BaseContentPage>

根据 Juan 的建议,这是正确的语法:

    <local:BaseContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:m="clr-namespace:MyProject.Models"
        xmlns:vm="clr-namespace:MyProject.ViewModels"
        x:Class="MyProject.Pages.MainPage"
        x:TypeArguments="vm:MainVm"
        xmlns:local="clr-namespace:MyProject">
        <ContentPage.Content>
            <StackLayout>
                <Label Text="Welcome to MyProject!"
                    VerticalOptions="CenterAndExpand" 
                    HorizontalOptions="CenterAndExpand" />
            </StackLayout>
        </ContentPage.Content>
    </local:BaseContentPage>

其中 x:TypeArguments="vm:MainVm" 定义泛型类型的参数。