0

I know that this had been answered multiple times before, and I've followed every possible guide. It doesn't work.

Here is my code:

XAML

<Label Text="{Binding Path=StatusMessage, Mode=TwoWay}"
       Margin="10,0,10,5"
       VerticalOptions="End"/>

C#

    private string statusMessage;
    public string StatusMessage { 
        get { return statusMessage; }
        set
        {
            statusMessage = value;
            OnPropertyChanged(nameof(StatusMessage));
        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

The class extends INotifyPropertyChanged and to modify the label text I tried both StatusMessage = "Status: ..."; and Device.BeginInvokeOnMainThread(() => { StatusMessage = "Status: ...";});.

Nothing works.

Any idea how to fix this mess?

EDIT

Adding BindingContext = this; as suggested in the main helped.

Now it won't update the label from code called from a different thread, as follows

private void OnEnableUser(bool authenticated)
{
    if (SynchronizationContext.Current != null)
    {
        [...]
    } else
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            OnEnableUser(authenticated);
        });
    }
}

1 Answer 1

2

Have you set the DataContext in the code behind?

this.DataContext = classWithStatusMessage;

2
  • In the class init i set BindingContext = this;. This kinda works when i set the text on OnAppearing(), but then it is not working anymore. I'm going to edit my question to point out one thing i missed.
    – Oiproks
    Apr 23, 2020 at 8:25
  • 1
    My bad. I was overwriting the label somewhere else.
    – Oiproks
    Apr 23, 2020 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.