Daily work with the platform Windows Phone brings many surprises. Very often you can meet with the situation, that many things are unspoken in the documentation and the programmer must by himself discover how something works.
Update Mango for Windows Phone introduced a new version of Silverlight for platform. This update has added the ability to use StringFormat the data binding in a file XAML. Using this additional option is very simple:
<TextBox Text="{Binding Path=Value, StringFormat=Currency: {0:c}}"/>
After this line you should see the number formatted as a currency. The result when you run this code do not fully correspond to what we expected. Yes, our number is formatted as a currency, but the name of the currency and number format is insensitive to change the location settings of phone.
Let’s analyze this issue further. To do this we need extra lines of code:
<StackPanel> <TextBox Text="{Binding Culture, StringFormat=Culture: {0}}"/> <TextBox Text="{Binding UICulture, StringFormat=UICulture: {0}}"/> <TextBox Text="{Binding Path=Value, StringFormat=Currency: {0:c}}"/> <TextBox Text="{Binding CurrentTime, StringFormat=DateTime: {0:G}}"/> </StackPanel>
And properties:
public string Culture { get { return Thread.CurrentThread.CurrentCulture.ToString(); } } public string UICulture { get { return Thread.CurrentThread.CurrentUICulture.ToString(); } } public double Value { get { return 12345.67; } } public DateTime CurrentTime { get { return DateTime.Now; } }
And now for the best output of the. When you start the emulator, the display language settings and regional settings is set to English (country United States). After starting the program we will get the following output: Up to this point everything is correct. Now let’s change the settings so, to language and region was associated with the Polish. It surprises that the result differs from our expectations:
On the one hand our application properly detects change the language settings. Application main thread, as well as the thread of UI correctly indicate, that the application should use the Polish formatting. However, both field gathers information about currency, as well as of the time does not respect these settings.
Repairing this error consists of adding one line to the constructor of the view:
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
After the restart we get the expected result.
A disgust remains. Thinking about the new applications I want to focus on its’ development, rather than coming up with workarounds to the problems of platform.
Miałem taki sam problem. Przydała mi się ta wskazówka. Dzięki!