How to change label text in xamarin

33,841

Solution 1

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.

Solution 2

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;

Solution 3

In order to update the UI, you have to be on the UI thread. You would want to do something like:

 Device.BeginInvokeOnMainThread(() =>
 {
     Label_ControlSW.Text = controlSW_Out;
     Label_BLESW.Text     = bleSW_Out;
     Label_Mode.Text      = mode_Out;
 });

This will solve your problem, but as the others have stated in their answers, the Xamarin way to do this would be using data binding to update the view. The data binding will handle the UI update for you.

Share:
33,841
Bleari
Author by

Bleari

Updated on January 23, 2021

Comments

  • Bleari
    Bleari over 3 years

    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"/>
    
  • Bleari
    Bleari almost 6 years
    The label just displays 'Initial Label' and not the variable
  • Bleari
    Bleari almost 6 years
    Sorry, what is a view folder?
  • Judson Abraham
    Judson Abraham almost 6 years
    View folder is the xaml file. So whenever you are creating any reference in Xaml you need to build it so that reference can be used in Xaml.cs file.
  • Judson Abraham
    Judson Abraham almost 6 years
    In your code the reference is x:Name="Label_ControlSW" if u have to access this in your code behind you need to build it.
  • Bleari
    Bleari almost 6 years
    Not sure I understand what you are saying, I set the label text in the code then build it, and there are no errors. Should I set it a different way? I also tried to do the MVVM approach, but I am not sure how to set the label text to what I need from mainpage.xaml.cs, this is updated every two seconds.
  • Bijington
    Bijington almost 6 years
    If you are going to suggest MVVM you might want to show a working example. You would need an INotifyPropertyChanged implementation in your ViewModel also. I appreciate it adds complexity but without this would not work.