"Errors Implementing a Simple .NET MAUI Page Inheritance"

GGGB-Bill

New member
Joined
Nov 24, 2024
Messages
4
Programming Experience
Beginner
I am a .Net Maui/programming newbie. I have a .NET Maui problem I have been struggling with all day: I set up a screen page, NewBasePage, that will be inherited by five different pages. I created one of those pages, PartsBuyDetail, to test it. I cannot get this to compile. What do I have to do to get the PartsBuyDetail page to inherit from NewBasePage? I sure would appreciate help.

Here are my compile errors:

Here are my errors:
Code:
Error Code	Description	File	Line #
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

Here’s my NewBasePage XMAL code:
XML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="[URL]http://schemas.microsoft.com/dotnet/2021/maui[/URL]"
             xmlns:x="[URL]http://schemas.microsoft.com/winfx/2009/xaml[/URL]"
             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>
Here’s my NewBasePage.xaml.CS code:
C#:
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:
C#:
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 code (for the page that is to inherit the NewBasePage):
XML:
<?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>
Here is my PartsBuyDetail.xaml.CS code (for the page that is to inherit the NewBasePage):
C#:
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 = "http://www.homedepot.com"]www.homedepot.com" },
                new PartsBuy { SourceName = "Lowes", SourceURL = "http://www.lowes.com"]www.lowes.com" }
            };
            ItemsCollectionView.ItemsSource = parts;
            DetailLabel.Text = "Details about buying parts.";
        }
    }
}
 
Last edited by a moderator:
WOW The way this site formats the information is heads above other sites ( assuming some moderator didn't have pitty my 'newbie-ness' and resubmit it on my behalf. This is my 1st ap... be gentle.
 
I was trying to fix up the formatting as best as as could.
 
I've not done anything with MAUI, but I believe that the designers of XAML also follow the modern object oriented programming rule of thumb: prefer composition over inheritance. So if you want to reuse parts of a page, that reusable part should go into a control or control template, and then the page that incorporates that control or control template.
 
Back
Top Bottom