4

I initially have a label setup in the MainPage.xaml file like this:

<Label x:Name="Ans"
       Text=""
       FontSize="18"
       FontFamily="Verdana"
       FontAttributes="Bold"
       TextColor="White"
       Grid.Column="1"
       Grid.Row="2" 
       Padding="20,20,0,0"/>

What I'm looking to do at runtime is to change the Text property to have a value.

I tried something like this in a class that I wrote:

string retVal;
retVal = "This is a test";
Ans.Text = retVal;

But I'm getting a message saying:

"The name 'Ans' does not exist in the current context."

How do I fix this?

4
  • 4
    Are you doing this in the code behind, or in some random unrelated class? Commented Jun 3, 2023 at 19:12
  • 2
    Why don't you use Binding instead? Commented Jun 3, 2023 at 19:56
  • Either use Binding, or use Messenger to send the new text to the page’s code behind, where you can do what you attempted. Commented Jun 3, 2023 at 20:15
  • @BobG Use this: learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm You will waste 3 months to learn how to use it, and then it will save you 3 years of fixing a code where you mixed your UI and logic. Commented Jun 5, 2023 at 10:20

2 Answers 2

4

You can only access the Label by its x:Name in the code-behind that belongs to the same XAML Page/View, in your case that would be MainPage.xaml.cs, e.g.:

public partial class MainPage : ContentPage
{
    public void ChangeLabel()
    {
        Ans.Text = "This is a test";
    }
}

You cannot access View elements from other classes by their x:Name attribute. XAML files are part of the same class as their code-behind, that's why the code-behind class is marked as partial.

A more common approach is to use the MVVM pattern and data binding instead of setting the Text property directly.

If you need to update the UI based on something that is not part of your View or ViewModel, e.g. a different, unrelated View or ViewModel, then you can also use the WeakReferenceMessenger to subscribe to messages and update the UI accordingly.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe your Label inside a CollectionView!.

I had the same problem before, and it was a "CollectionView" problem. let me explane, not just a Label but whatever Element inside a CollectionView, its x:name="whatever" will not be known in code-behind. knowing that you should consider maybe other type of UI Collections also have the same effects on it's inside Elements, idk.

I've tested all that, make a new ContentPage and replace it's xaml with this down here (don't forget to edit the namespace).

<?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="someNamespace.TestPage"
         Title="TestPage">
<VerticalStackLayout>        
    <CollectionView>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Label x:Name="LabelInsideCollectionView"
                       Text="Welcome to .NET MAUI!"
                       VerticalOptions="Center"
                       HorizontalOptions="Center" />
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    
    <Label x:Name="LabelOutsideCollectionView"
           Text="Welcome to .NET MAUI!"
           VerticalOptions="Center"
           HorizontalOptions="Center" />
    
</VerticalStackLayout>
</ContentPage>

Here's the code-behind.

namespace someNamespace;
public partial class TestPage : ContentPage
    {
        public TestPage()
        {
            InitializeComponent();

            // This work.
            LabelOutsideCollectionView.Text = "Test";

            // this will not work, Error CS0103  The name 
           //'LabelInsideCollectionView' does not exist in the current context
            LabelInsideCollectionView.Text = "Test"; 
        }
    }

you will see that your code-behind can read "LabelOutsideCollectionView" but not "LabelInsideCollectionView".

I hope it help you or others, and hope MAUI team can fix all that.

Here's a list of the installed Versions on my machine as of the time of this Answer.

Installed Workload Id      Manifest Version       Installation Source
---------------------------------------------------------------------
android                    33.0.68/7.0.100        VS 17.7.33906.173
maui-android               7.0.92/7.0.100         VS 17.7.33906.173
maui-windows               7.0.92/7.0.100         VS 17.7.33906.173
maui-maccatalyst           7.0.92/7.0.100         VS 17.7.33906.173
maccatalyst                16.4.7089/7.0.100      VS 17.7.33906.173
maui-ios                   7.0.92/7.0.100         VS 17.7.33906.173
ios                        16.4.7089/7.0.100      VS 17.7.33906.173

I've reported just now an issue to the repository of.NET MAUI on Github. https://github.com/dotnet/maui/issues/16182

Comments

Your Answer

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