0

In my Windows Phone 8 application I have a text box which uses InputScopeNameValue.Number to elicit a currency amount from the user.

The OnBackKeyPress override is parsing the string value of the text box into a decimal value:

    base.OnBackKeyPress(e);
    decimal price = 0m;

    var value = PriceControl.PropertyValue;
    NumberStyles style = NumberStyles.Number;
    CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentCulture;

    if (string.IsNullOrEmpty(PriceControl.PropertyValue) || 
        decimal.TryParse(value, style, culture, out price))
    {
        App.ViewModel.SelectedProduct.Price = price;
    }
    else
    {
        e.Cancel = true;
    }

Everything works as expected for en-US, but once I switch the Region setting of the Windows Phone emulator to de-CH (Switzerland, but other European cultures such as de-DE have the same issue) the following things happen:

  • The on-screen keyboard now shows a ',' (comma) rather than a '.' (period) along with keys for the digits

  • The culture.NumberFormat.CurrrencyDecimal.Separator is still '.' as is the culture.NumberFormat.NumberDecimalSeparator (period). Therefore the decimal number entered using the keyboard cannot be successfully parsed!

Is there a way around this? The mismatch between the separator symbol on the keyboard (comma) and the separator in the corresponding NumberFormat seems strange.

Philipp Schmid
  • 5,778
  • 5
  • 44
  • 66

1 Answers1

0

In your decimal.TryParse use CultureInfo.InvariantCulture instead of current culture.

Filip
  • 1,824
  • 4
  • 18
  • 37