Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change label text in xamarin

I am relatively new to Xamarin forms. I have found out I am unable to change label text from the code behind. Normally I would do myLabel.text = variable. Does this work in Xamarin? If it does why does this code not change the text?

Label_ControlSW.Text = controlSW_Out;
            Label_BLESW.Text = bleSW_Out;
            Label_Mode.Text = mode_Out;

Xaml file

<Label x:Name="Label_ControlSW" Grid.Row="1" Grid.Column="1" HorizontalOptions="Center"  VerticalOptions="Center" FontSize="17" TextColor="White"/>
                <Label x:Name="Label_BLESW" Grid.Row="2" Grid.Column="1" HorizontalOptions="Center"  VerticalOptions="Center" FontSize="17" TextColor="#525252"/>
                <Label x:Name="Label_Mode"  Grid.Row="4" Grid.Column="1" HorizontalOptions="Center"  VerticalOptions="Center" FontSize="17" TextColor="White"/>
like image 240
Bleari Avatar asked Sep 17 '18 11:09

Bleari


People also ask

What is Label in Xamarin forms?

A Label is used to display single-line text elements as well as multi-line blocks of text. The following example, adapted from the default Xamarin.Forms solution, shows a basic use: C# Copy.


Video Answer


2 Answers

Does this work in Xamarin?

Yes, it does.

If it does why does this code not change the text?

Because the Label component is not bounded to the variable, it just gets its value when you did Label_ControlSW.Text = controlSW_Out; and no furthermore.

To make it works you have basically two choices:

1. Set the value to the label on every change;

There's no magic here. Just set the values or variables like Ali Heikal's answer suggests, but you must do that every time manually.

2. Bind the page (View) to an Observable object (Model), then the view will listen to every change on your model and react to this (changing it's own Text value, for example).

I guess what you're intending to do is the second one. So you can create a public string property on your page's code-behind and bind the instance of your page to itself. Like this:

XAML

<Label Text="{Binding MyStringProperty}"
       .../>

Code behind

public partial class MyTestPage : ContentPage
{
    private string myStringProperty;
    public string MyStringProperty
    {
        get { return myStringProperty; }
        set 
        {
            myStringProperty = value;
            OnPropertyChanged(nameof(MyStringProperty)); // Notify that there was a change on this property
        }
    }
    
    public MyTestPage()
    {
        InitializeComponents();
        BindingContext = this;

        MyStringProperty = "New label text"; // It will be shown at your label
    }
}

You should take a look at official docs about data bindings and MVVM pattern on XF and if you're starting with Xamarin.Forms, I highly recommend you to follow the official getting started guide that addresses each topic clear and deep enough to learn everything you need.

I hope it helps.

like image 50
Diego Rafael Souza Avatar answered Oct 04 '22 15:10

Diego Rafael Souza


Try initializing the Text value in XAML like the following:

<Label x:Name="YourLableName" Text="Initial Label"/>

Then access it in the code behind like the following:

YourLableName.Text = "Desired Name";

or

YourLableName.Text = variable;
like image 27
Ali Heikal Avatar answered Oct 04 '22 13:10

Ali Heikal