2

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

2
  • 1
    You have already defined your controls in the XAML. You don’t need to do it again in code. Commented Nov 25, 2024 at 3:45
  • When you declare a name to a control in XAML it will generate a private field. If you change the name to titleLabel in the XAML you can expose it as a property public Label TitleLabel => titleLabel; Commented Nov 25, 2024 at 6:32

1 Answer 1

0

The comment about a named control being a "private field" is correct. Here is how I fixed this.

In NewBasePage.xaml, for each named item (TitleLabel, TaskTitleLabel, ItemsCollectionView, and DetailLabel), add

x:FieldModifier="protected"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.