Xamarin – How to set the name of a Label in Xamarin Forms

xamarinxamarin.forms

I'm trying to add a name to a label so I can find it using the Content.FindByName<>() method

as far as I can tell the Xamarin.Forms.Label() class doesn't have a Name property

Here is my page class

public class HelloWordCodePage : ContentPage
{
    public Label HelloWorldLabel;

    public HelloWordCodePage()
    {
        HelloWorldLabel = new Label{Text = "Hello World", };
        ConstructView();
    }

    public void ConstructView()
    {
        Content = new StackLayout
        {
            Children =
            {
                HelloWorldLabel
            }
        };
    }
}

Best Solution

If you define your label in Xaml, you can use this syntex:

<Label x:Name="YourLableName" Text="A simple Label" />

And then access it in the code behind like this

YourLableName.Text = "bla bla"

However, if you don't use Xaml, and all your page definition is found in the code behind, you should keep a reference to that label in order to access it instead of finding it using Content.FindByName<>() as @Sten Petrov commented

Related Question