MAUI - Access XAML element from a different class code behind

Toman, Martin 0 Reputation points
2025-11-18T06:03:24.82+00:00

I'm working on my first MAUI Android app. It consists of two screens, switchable via a tab control.

Now to my issue - I have page MainPage.xaml with MainPage.xaml.cs and also AboutPage.xaml with AboutPage.xaml.cs. Is there a way how to access an element of AboutPage.xaml from MainPage.xaml.cs?

My goal is, when I press a button on MainPage.xaml, I want to write a text on AboutPage.xaml, so that it's there when user switches to AboutPage via the tab.

In my AboutPage.xaml, I have

<Label 
    x:Name="textBox1"
    x:FieldModifier="public"
    Text="This a test app." />

Then, in MainPage.xaml.cs I would like to do something like

private void OnCounterClicked(object? sender, EventArgs e)
 {
     count++;
     changeAboutText();
 }

public void changeAboutText()
{
    var aboutPage = new AboutPage();
    aboutPage.textBox1.Text = "hello";
}

This does not say any errors when building, but it does not change the text to "hello" on the AboutPage.

Any ideas? I've tried the AI advised solution and still does not work.

Thanks a lot in advance for any hint!

BR, Martin

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-18T06:03:50.2733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    In .NET MAUI, when you create a new instance of a page (like AboutPage), it does not share the state or UI elements with the existing instance that is already displayed. This is why setting aboutPage.textBox1.Text does not update the Label on the AboutPage that is currently shown to the user.

    To achieve your goal of updating the Label on the AboutPage from the MainPage, you can use a shared property or method that allows you to modify the Label text directly on the existing instance of AboutPage. Here’s how you can do it:

    1. Create a public method in AboutPage.xaml.cs to update the text:
         public void UpdateText(string newText)
         {
             textBox1.Text = newText;
         }
      
    2. Store a reference to the AboutPage instance when it is created: You can do this in the MainPage constructor or wherever you instantiate your pages.
         private AboutPage aboutPage;
         
         public MainPage()
         {
             InitializeComponent();
             aboutPage = new AboutPage(); // Create an instance of AboutPage
         }
      
    3. Call the UpdateText method from MainPage.xaml.cs:
         private void OnCounterClicked(object? sender, EventArgs e)
         {
             count++;
             aboutPage.UpdateText("hello"); // Update the text on the existing AboutPage instance
         }
      

    This way, when you press the button on MainPage, it will update the Label on the existing AboutPage instance that is displayed in the tab control.


    References:

    0 comments No comments

  2. Jack Dang (WICLOUD CORPORATION) 3,965 Reputation points Microsoft External Staff Moderator
    2025-11-18T08:44:55.0966667+00:00

    Hi @Toman, Martin ,

    Thanks for reaching out.

    Based on your description, you want to update a Label on AboutPage.xaml when a button on MainPage.xaml is pressed, and you are using a TabbedPage where both pages are already created by the Shell.

    Below is a working example that recreates your scenario. The key point is:

    you must reference the existing AboutPage instance created by the Shell, not create a new AboutPage inside MainPage.

    AboutPage.xaml

    <?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="MauiTabDemo.AboutPage"
                 Title="AboutPage">
        <StackLayout Padding="30">
            <Label x:Name="textBox1"
                   x:FieldModifier="public"
                   Text="This is a test app." />
        </StackLayout>
    </ContentPage>
    

    AppShell.xaml

    <TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:MauiTabDemo"
                x:Class="MauiTabDemo.AppShell">
        <local:MainPage Title="Main Page" />
        <local:AboutPage Title="About Page" />
    </TabbedPage>
    

    AppShell.xaml.cs

    namespace MauiTabDemo
    {
        public partial class AppShell : TabbedPage
        {
            public AboutPage AboutPageInstance { get; private set; }
            public AppShell()
            {
                InitializeComponent();
                // Grab the existing AboutPage instance
                AboutPageInstance = this.Children.OfType<AboutPage>().FirstOrDefault();
            }
        }
    }
    

    MainPage.xaml

    <?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="MauiTabDemo.MainPage">
        <VerticalStackLayout Padding="30">
            <Button Text="Change About Page Text"
                    Clicked="OnCounterClicked"/>
        </VerticalStackLayout>
    </ContentPage>
    

    MainPage.xaml.cs

    namespace MauiTabDemo
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void OnCounterClicked(object sender, EventArgs e)
            {
                // Get the AppShell instance
                var shell = Application.Current.MainPage as AppShell;
                if (shell?.AboutPageInstance != null)
                {
                    shell.AboutPageInstance.textBox1.Text = "Hello from MainPage!";
                }
            }
        }
    }
    

    In this approach:

    • Shell creates both MainPage and AboutPage when the TabbedPage loads.
    • You retrieve the already-created AboutPage instance from AppShell.Children.
    • Updating the Label on that instance updates the UI immediately when the user switches to the tab.

    Although the above code works, it is not considered best practice in .NET MAUI because:

    • MainPage directly reaches into AboutPage’s UI controls (textBox1). This breaks encapsulation and makes the app harder to maintain.
    • If you add more pages or navigation paths, this approach becomes fragile.

    The recommended MAUI architecture is MVVM where:

    • Pages bind to a shared ViewModel
    • Pages do not manipulate each other
    • UI updates happen through data binding

    So my recommendation for your project is to use a shared ViewModel and update a property instead of touching controls. Both MainPage and AboutPage bind to the same ViewModel instance. This is clean, scalable, and testable.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.