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
Bindinginstead?