5

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>
sinsedrix
  • 4,336
  • 4
  • 29
  • 53
  • Did you also change the base class on the XAML? They should match – Juan Sturla Oct 06 '21 at 15:16
  • @JuanSturla How can I specify the generic value for the `BaseContentPage` markup? – sinsedrix Oct 06 '21 at 16:09
  • I don't know. MVVMCross does something like that. In the code-behind it's something like `MyPage: MvxContentPage` and in the XAML it's `` – Juan Sturla Oct 06 '21 at 16:17
  • You could poat your MAUI issue in the link below to get more support. https://github.com/dotnet/maui/issues – Wendy Zang - MSFT Oct 07 '21 at 05:39
  • @WendyZang-MSFT https://github.com/dotnet/maui/issues/2864 – sinsedrix Oct 07 '21 at 08:27
  • @JuanSturla Is MVVMCross adapted to MAUI, isn't it dependent on Xamarin? – sinsedrix Oct 07 '21 at 08:39
  • Avalonia uses ReactiveUI to implement generic views, but doesn't generate code behind, so there's no conflict. I don't know if ReactiveUI would fit to MAUI. – sinsedrix Oct 07 '21 at 08:49
  • @sinsedrix yes, MVVMcross depends on Xamarin. But I thought that it might work too. It looks like it doesn't :/ – Juan Sturla Oct 07 '21 at 09:45
  • 2
    And if you replace `BaseContentPage` with `local:BaseContentPage` in your XAML? – Juan Sturla Oct 07 '21 at 09:46
  • I'm trying something similar with ContentViews. A key difference is the ContentView automatically gets the BindingContext of the ContentPage. I'm getting an error: "The type 'local:OrientationContentViewLoader' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built." Do you have any idea why your solution doesn't work for me? Here's my posting: https://stackoverflow.com/questions/74972784/maui-contentview-cant-inherit-from-custom-base-class?noredirect=1#comment132311281_74972784 – David Pugh Jan 01 '23 at 18:54

1 Answers1

8

According to Juan advice, here is the righ syntax:

    <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>

Where x:TypeArguments="vm:MainVm" defines the argument for the generic type.

sinsedrix
  • 4,336
  • 4
  • 29
  • 53