I want my page classes inherit from the following base 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();
}
}
Page classes are partial and I suppose, MAUI generates some hidden code with a different parent class. Then I get the following error:
CS0263: Partial declarations of 'type' must not specify different base classes
Is there a way to specify the parent class of the generated partial class?
EDIT
- First, I was keeping the original markup, which generate a different base class and leads to the CS0263 error:
<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>
- Then I tried tu use my generic base class with TypeArguments as mentioned by @JuanSturla, but this leads to an unknown class error:
<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>
BaseContentPage
markup? – CandlepowerMyPage: MvxContentPage<ViewModel>
and in the XAML it's<views:MvxContentPage xmlns:viewModels="clr-namespace:MyMvvmCrossApp1.Core.ViewModels.Contacts;assembly=MyMvvmCrossApp1.Core" x:TypeArguments="viewModels:ContactsViewModel">
– HistidineBaseContentPage
withlocal:BaseContentPage
in your XAML? – Histidine