1
votes

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.

2
if you add XAlign = "Center" in XAML, does that make any difference?Carlos Rodrigez
You may want to wrap your label inside a STACKLAYOUT where you FORCE it to be centered through your app width, instead of trying to align the individual labelSergioAMG
Is this on Android specifically because I have found on many occasions HorizontalText/VerticalTextAlignment don't always behave correctly on things that dynamically update. I have also noticed this issue seems a bit worse with Forms 2.3.4 so most of the time I've just switched to Horizontal/VerticalOptions even though they're slightly more performance intensive.Nick Peppers

2 Answers

2
votes

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.

-1
votes

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.