I have a .NET Maui problem. I have been struggling with all day: I am creating my first app, a simple Home Maintenance Tracking application. I have 4 very similar screens, so I decided to implement a page class so these similar pages could inherit the characteristics from the parent or base page. I set up a page, NewBasePage, that 5 pages will inherit. I created one of those pages, the PartsBuyDetail page, to test. I cannot get this to compile.
What must I do to get the PartsBuyDetail page to inherit the NewBasePage?
Here's my NewBasePage XAML markup:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HomeUpKeepPro.Views.NewBasePage">
<StackLayout Padding="10">
<Label FontSize="24" HorizontalOptions="Center" x:Name="TitleLabel" />
<Label FontSize="18" HorizontalOptions="Center" x:Name="TaskTitleLabel" />
<CollectionView x:Name="ItemsCollectionView">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" Padding="5">
<Label Text="{Binding SourceName}" FontSize="16" />
<Label Text="{Binding SourceURL}" FontSize="16"
TextColor="Blue" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Label x:Name="DetailLabel" />
<Button Text="Add" Clicked="OnAddClicked" />
<Button Text="Save" Clicked="OnSaveClicked" />
<Button Text="Delete" Clicked="OnDeleteClicked" />
</StackLayout>
</ContentPage>
This is my NewBasePage.xaml.cs code:
using Microsoft.Maui.Controls;
namespace HomeUpKeepPro.Views
{
public partial class NewBasePage : ContentPage
{
public NewBasePage()
{
InitializeComponent();
}
protected void OnAddClicked(object sender, EventArgs e)
{
// Handle the Add button click event
}
protected void OnSaveClicked(object sender, EventArgs e)
{
// Handle the Save button click event
}
protected void OnDeleteClicked(object sender, EventArgs e)
{
// Handle the Delete button click event
}
}
}
Note: when I added:
protected Label TitleLabel { get; private set; }
protected Label TaskTitleLabel { get; private set; }
protected CollectionView ItemsCollectionView { get; private set; }
protected Label DetailLabel { get; private set; }
above the public NewBasePage() line, 36 errors occurred, essentially saying the type NewBasePage already contains a definition for the labels and collectionview and ambiguity between NewBasePage.TitleLabel and NewBasePage.TitleLabel.
Here is my PartsBuyDetail.xaml markup (for the page that is to inherit from NewBasePage):
<?xml version="1.0" encoding="utf-8" ?>
<views:NewBasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:HomeUpKeepPro.Views"
x:Class="HomeUpKeepPro.Views.PartsBuyDetail">
</views:NewBasePage>
And this finally is my PartsBuyDetail.xaml.cs code (for the page that is to inherit from NewBasePage):
using Microsoft.Maui.Controls;
using System.Collections.ObjectModel;
using HomeUpKeepPro.Models;
namespace HomeUpKeepPro.Views
{
public partial class PartsBuyDetail : NewBasePage
{
public PartsBuyDetail()
{
InitializeComponent();
// Populate data in code-behind
TitleLabel.Text = "Parts Buy Detail";
TaskTitleLabel.Text = "Task: Buy Parts";
var parts = new ObservableCollection<PartsBuy>
{
new PartsBuy { SourceName = "Home Depot", SourceURL = "www.homedepot.com" },
new PartsBuy { SourceName = "Lowes", SourceURL = "www.lowes.com" }
};
ItemsCollectionView.ItemsSource = parts;
DetailLabel.Text = "Details about buying parts.";
}
}
}
These are the errors I get :
CS0122 'NewBasePage.TitleLabel' is inaccessible due to its protection level PartsBuyDetail.Xaml.cs 13
CS0122 'NewBasePage.TaskTitleLabel' is inaccessible due to its protection level PartsBuyDetail.Xaml.cs 14
CS0122 “.ItemsCollectionView' is inaccessible due to its protection level PartsBuyDetail.Xaml.cs 20
CS0122 'NewBasePage.DetailLabel' is inaccessible due to its protection level PartsBuyDetail.Xaml.cs 21
titleLabelin the XAML you can expose it as a propertypublic Label TitleLabel => titleLabel;