Uros
Uros

Reputation: 2150

Label not centered after data change in Xamarin Forms

In my Xamarin Forms application I have Label:

<Label Text="{Binding Number}" HorizontalTextAlignment ="Center">

In ViewModel:

private int _number;
public int Number
{
    get { return _number; }
    set { SetProperty(ref _number, value); }
}

When message is received, I update number:

MessagingCenter.Subscribe<EmptyMessage>(this, "NUMBER_CHANGED", OnNumberChanged);

private async void OnNumberChanged(EmptyMessage emptyMessage)
{
    Number = ReadNumberFromDB();
}

All of this is on MainPage. When "NUMBER_CHANGED" message is triggered from MainPage, Number is updated on UI and label containing Number is properly centered.

From MainPage, one can PushAsync SecondPage. From SecondPage "NUMBER_CHANGED" can also be triggered. After triggering it and returning to MainPage, Number is updated, label containing it has proper value but instead of being centered it is left aligned.

Is this Xamarin UI bug? Is there a workaround for this? I need somehow to tell label to refresh it's position. I want label to always be centered. I don't know why it is left aligned after refreshing it's content.

Upvotes: 1

Views: 877

Answers (2)

root237
root237

Reputation: 169

I ran into this problem recently. I simply made a constant variable to track what page needed to be updated OnResume and just reassigned my bindings accordingly.

Upvotes: -1

Adam
Adam

Reputation: 16199

HorizontalTextAlignment and VerticalTextAlignment, not working after a data change is a known bug. Bug 55359 details this for a TabbedPage, but it occurs in many places, such as Bug 49311.

The workaround is to use HorizontalOptions or VerticalOptions for the moment.

Upvotes: 2

Related Questions