Let MAUI page inherit form custom base class
Asked Answered
C

1

10

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>
Candlepower answered 6/10, 2021 at 15:13 Comment(10)
Did you also change the base class on the XAML? They should matchHistidine
@JuanSturla How can I specify the generic value for the BaseContentPage markup?Candlepower
I don't know. MVVMCross does something like that. In the code-behind it's something like MyPage: 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">Histidine
You could poat your MAUI issue in the link below to get more support. github.com/dotnet/maui/issuesMelanie
@WendyZang-MSFT github.com/dotnet/maui/issues/2864Candlepower
@JuanSturla Is MVVMCross adapted to MAUI, isn't it dependent on Xamarin?Candlepower
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.Candlepower
@Candlepower yes, MVVMcross depends on Xamarin. But I thought that it might work too. It looks like it doesn't :/Histidine
And if you replace BaseContentPage with local:BaseContentPage in your XAML?Histidine
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: #74973284Trigonometry
C
12

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.

Candlepower answered 7/10, 2021 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.