Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] Entry shows incorrect value when binding against a float property with StringFormat #10119

Open
crenan opened this issue Mar 28, 2020 · 5 comments

Comments

@crenan
Copy link

crenan commented Mar 28, 2020

Description

Xamarin Entry views are not working properly when using StringFormat in the binding configuration (e.g. 'F4') for a writable float property combined with an Android using a language other than US English (e.g. Portuguese Brazil).

Steps to Reproduce

  1. Create a new AVD
    -> I used Nexus S
    -> API anything >= 25
    -> +Google APIs (does not matter but I use it anyways)

  2. Start the AVD and configure the language for Português Brasil as main language
    -> Settings -> System -> Languages & Input -> Languages -> Add Language
    -> Choose Português, then Brasil as "suggested"
    -> Move the new language to top

  3. Create a new Xamarin Forms project
    -> Blank
    -> Android (I don't have a Mac to test with iOS)

  4. Update the solution's NuGet packages for the latest versions
    -> NetStandard.Library v2.0.3
    -> Xamarin.Essentials v1.5.1
    -> Xamarin.Forms v4.5.0.495

  5. Create a new ViewModel class in the Class Library project that implements INotifyPropertyChanged

public class MainPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
}
  1. Add a public property of type float using a private member
private float _vmFloatProperty = 0.567F;
public float VMFloatProperty
{
    get => _vmFloatProperty;
    set
    {
        _vmFloatProperty = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(VMFloatProperty)));
    }
}
  1. Modify the MainPage.xaml.
    -> Include the ContextBinding configuration to use the created ViewModel class
<ContentPage.BindingContext>
    <viewModel:MainPageViewModel/>
</ContentPage.BindingContext>

-> Include an Entry and bind the Text property to the float property of the ViewModel class with a StringFormat

<Entry Text="{Binding VMFloatProperty, StringFormat='{0:F4}'}" IsEnabled="False"/>
  1. Start the app.

Expected Behavior

The Entry view should display the value in the correct format specified in the Binding configuration regardless of the OS culture.

Actual Behavior

The Entry view is trying to format the value using the OS culture defined. This is causing a wrong parsing with the value.
Also, if the property is displayed more than one time, the value starts to increase.

Basic Information

  • Version with issue: v4.5.0.495
  • Last known good version: Don't know
  • IDE: Visual Studio 2019 v16.5.0
  • Platform Target Frameworks:
    • iOS: None
    • Android: 7.1, 8.0, 8.1, 9.0
    • UWP: None
  • Android SDK Tools v26.1.1
  • Android SDK Platform-Tools v29.0.5
  • Android SDK Build Tools v29.0.2 (also v28.0.3 installed)
  • Android Emulator v29.3.0
  • Nuget Packages: Only those installed with the template project (VS 2019 Xamarin Forms)
  • Affected Devices: Tested with AVDs using API >= 25

Screenshots

Android Previewer is also affected.

FloatPropertyError

Reproduction Link

EntryFloatBug.zip

Workaround

-> Use read-only properties.
-> Use Mode=OneWay in binding configuration.
-> Using a property of a class that does not implement INotifyPropertyChanged is the same as accessing a read-only property.

@crenan crenan added s/unverified New report that has yet to be verified t/bug 🐛 labels Mar 28, 2020
@pauldipietro pauldipietro added this to New in Triage Mar 28, 2020
@StephaneDelcroix
Copy link
Member

StephaneDelcroix commented Mar 30, 2020

binding are supposed to use the CurrentUICulture for conversion, can you check what yours is set to ?

@crenan
Copy link
Author

crenan commented Mar 30, 2020

binding are supposed to use the CurrentUICulture for conversion, can you check what yours is set to ?

Hi, @StephaneDelcroix. Mine is set to "Português Brasil" (pt-br), as stated in the "Steps to Reproduce" above.

@kingces95
Copy link
Contributor

kingces95 commented Apr 2, 2020

I'm able to run the reproduction but my screenshot doesn't match yours. Could you be more specific about the expected and actual values? Could you either generate screenshots of actual and expected values and/or quote the values you expect and see?

@crenan
Copy link
Author

crenan commented Apr 2, 2020

Hey @kingces95!
Using the test project, the expected value is "0,5670" for ALL Entry views.
The actual values are different depending the way the Entry binding was configured and which kind of property it was bound.
If you take a look at the screenshot I sent, the correct value was displayed only in the 2nd, 3rd and last Entry view.

I'm able to run the reproduction but my screenshot doesn't match yours.

You need to set the language in the AVD to pt-br (Portuguese Brazil) or in your development O.S to see in Android Previewer.

@kingces95
Copy link
Contributor

Reproduces as described.

@kingces95 kingces95 added p/Android and removed s/unverified New report that has yet to be verified labels Apr 3, 2020
@kingces95 kingces95 moved this from New to Ready For Work in Triage Apr 3, 2020
@samhouts samhouts moved this from Ready For Work to Needs Estimate in Triage Apr 10, 2020
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@samhouts samhouts added this to To do in vNext+1 (5.0.0) Aug 13, 2020
@samhouts samhouts removed this from the 5.0.0 milestone Nov 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Triage
  
Needs Estimate
Development

No branches or pull requests

4 participants