In my MAUI project, I have a lot of views as ContentPage that share some common functions. So, I want to create a base class with those functions to use across the ContentPages.
Every page has different viewmodels, UI and all the usual components (like DisplayAlert). Based on another post and this one, I wrote my base class
public abstract class BaseContentPageClass<T> : ContentPage
where T : BaseViewModel
{
public BaseContentPageClass(T viewModel, string pageTitle)
{
BindingContext = ViewModel = viewModel;
Title = pageTitle;
}
protected T ViewModel { get; }
}
Then, I like to have a base class for the ContentView that has to display advertisements, for example. So, I created another class BaseAdvClass based on the base class
public partial class BaseAdvClass<T> :
BaseContentPageClass<T> where T : BaseViewModel
{
#region Variables
public bool ShouldSetEvents { get; set; } = true;
public long Points { get; set; } = 100;
private readonly UserSettings settings;
#endregion
public BaseAdvClass(T viewModel, string pageTitle, UserSettings settings)
: base(viewModel, pageTitle)
{
this.settings = settings;
InitializeComponent();
}
}
Then, I created a new ContentPage named ProfilePage and this is the XAML and the code behind:
<local:BaseAdvClass xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:lang="clr-namespace:MyProject"
x:Class="MyProject.Views.ProfilePage"
xmlns:local="clr-namespace:MyProject.Views">
public class ProfilePage : BaseAdvClass<ProfilePageViewModel>
{
ProfilePageViewModel vm;
private bool _shouldSetEvents = true;
public ProfilePage(ProfilePageViewModel model, UserSettings settings)
: base(model, "", settings)
{
}
// ...
}
At this point the build raises a lot of errors:
- Using the generic type 'BaseAdvClass' requires 1 type arguments (LanguageInUse)
- in the code behind for the base, it requires the class as
partialbut when I addpartialI get another error
- in this page
DisplayAlertdoesn't exist in the current context - in the
BaseAdvClass, InitializeComponent doesn't exist in the current context - if I add in the constructor of
BaseContentPageClasstheInitializeComponentI get the same error (doesn't exist in the current context)

