Object.ToString() Using Invariant Culture

by Weifen Luo (DevZest) 17. September 2009 23:29

Today, an Italian user of WPF Docking reported a bug in BrowserDockSample: the saved floating window position is not correctly loaded - it seems the loaded floating window is displayed at far right bottom of the screen. Taking the saved XAML text under the Italian culture:

<DockLayout
  xmlns="http://schemas.devzest.com/presentation/docking"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  LeftDockTreeWidth="0.333333333333333*"
  RightDockTreeWidth="0.333333333333333*"
  TopDockTreeHeight="0.333333333333333*"
  BottomDockTreeHeight="0.333333333333333*"
  DockTreeZOrder="Right, Left, Top, Bottom">
  <DockLayout.DockItems>
    <DockItemReference AutoHideSize="0.333333333333333*">DevZest.DockSample.Output</DockItemReference>
    <DockItemReference AutoHideSize="0.333333333333333*">DevZest.DockSample.SavedLayout</DockItemReference>
    <DockItemReference AutoHideSize="0.333333333333333*">DevZest.DockSample.PropertiesWindow</DockItemReference>
    <DockItemReference AutoHideSize="0.333333333333333*">DevZest.DockSample.SolutionExplorer</DockItemReference>
    <DockItemReference AutoHideSize="0.333333333333333*">DevZest.DockSample.Welcome</DockItemReference>
  </DockLayout.DockItems>
  <DockLayout.ShowActions>
    <ShowAsDockPositionAction DockPosition="Right" Source="2" ShowMethod="Hide" />
    <ShowAsTabbedAction Target="2" Source="3" ShowMethod="Hide" />
    <ShowAsDockPositionAction DockPosition="BottomAutoHide" Source="1" />
    <ShowAsSidePaneAction Side="Left" Size="*" IsSizeForTarget="True" Target="1" Source="0" />
    <ShowAsDockPositionAction DockPosition="Document" Source="4" />
    <ShowAsFloatingAction Left="757,391520253099" Top="95,8056683888685" Width="300" Height="300" Source="2" />
    <ShowAsTabbedAction Target="2" IsFloating="True" Source="3" />
  </DockLayout.ShowActions>
</DockLayout>

The Left and Top of floating window position is persisted as 757,391520253099 and 95,8056683888685 instead of 757.391520253099 and 95.8056683888685. The decimal symbol in Italian (and most European languages)  is comma, not point. After investigation, the problem is the following code in DockSample project:

string temp = markupProperty.Value == null ? string.Empty : markupProperty.Value.ToString();

Object.ToString() uses current culture. For serialization, invariant culture should always be used. The System.Runtime.Serialization.FormatterConverter class is provided exactly for this purpose. So the above code is modified as:

using System.Runtime.Serialization;
... private FormatterConverter _formatterConverter = new FormatterConverter();
... string temp = markupProperty.Value == null ? string.Empty : _formatterConverter.ToString(markupProperty.Value);

Problem solved!

PS: I searched the internet before finding this solution and came across this thread. Unfortunately it does not provide the correct solution and is closed.

Tags:

.Net

Comments

9/17/2009 11:33:28 PM #

Object.ToString() Using Invariant Culture

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com |

Comments are closed

Copyright DevZest, 2008 - 2017