我遇到了一个奇怪的问题。我的应用程序已经本地化,支持5种语言。这是一个日历类的应用程序,我可以选择为特定的日期写笔记。因此,当我将语言设置更改为俄语时,请在日期上输入注释,例如2012年1月3日和2012年6月3日。这个日期和笔记存储在IsolatedStorageFile中,然后我退出应用程序。
现在,我将我的语言设置更改为英语,备注存储在2012年3月1日和2012年3月6日。月份和日期颠倒了!!:(
这就是我向文件中写入注释的方式
using (IsolatedStorageFileStream fileStream = storagefile.OpenFile("NotesFile", FileMode.Open, FileAccess.ReadWrite))
{
StreamWriter writer = new StreamWriter(fileStream);
for (int i = 0; i < m_noteCount; i++)
{
writer.Write(m_arrNoteDate[i].ToShortDateString());
writer.Write(" ");
writer.Write(m_arrNoteString[i]);
writer.WriteLine("~`");
}
writer.Close();
}从文件中读取是这样完成的
if (notes.Substring(i, 1) == " ")
{
m_arrNoteString[count] = notes.Substring(i + 1);
string temp = notes.Substring(0, i);
m_arrNoteDate[count] = DateTime.Parse(temp);
count++;
nextCharacter = (char)reader.Read();
notes = "";
break;
}解析器根据设备设置的语言读取数据。有什么解决办法吗?
阿尔法
发布于 2012-03-06 23:27:16
默认情况下,日期和字符串之间的转换(反之亦然)使用当前区域性。出于您的目的,由于您不是为了向用户显示日期而是为了持久化它们而是为了将字符串转换为字符串,因此应该通过指定给定的区域性来使您的逻辑独立于当前区域性。不变的文化是你最好的最好的。
因此,请替换以下内容:
writer.Write(m_arrNoteDate[i].ToShortDateString());有了这个:
writer.Write(m_arrNoteDate[i].ToString("d", CultureInfo.InvariantCulture);并替换为:
m_arrNoteDate[count] = DateTime.Parse(temp);有了这个:
m_arrNoteDate[count] = DateTime.Parse(temp, CultureInfo.InvariantCulture);发布于 2012-03-06 23:25:04
DateTime.Parse有一个重载方法,您可以通过传递CultureInfo.InvariantCulture独立于区域设置来读取它。此外,您还需要相应地保存值。
另外,看看Thread.CurrentThread.CurrentCulture和Thread.CurrentThread.CurrentUICulture。您可以全局设置应用程序的特定区域性。
发布于 2012-03-06 23:25:05
在写入日期时使用System.Globalization.CultureInfo.InvariantCulture) (“d”,m_arrNoteDatei.ToString)使其独立于线程区域性。
https://stackoverflow.com/questions/9586335
复制相似问题