gpt4 book ai didi

c# - WPF 默认日期时间格式

转载 作者:行者123 更新时间:2023-11-30 16:55:12 24 4
gpt4 key购买 nike

在大型 WPF 应用程序上工作并且 DateTime 格式存在一些问题,这是一个庞大的代码库,许多地方没有指定区域性。

当用户在 Windows 7 或 8 上打开应用程序时,日期时间格式不同(Win 7 使用斜杠,Win 8 使用破折号)。

我尝试在应用程序启动中将文化设置为“en-US”(参见下面的链接),但它似乎不起作用? Setting Culture (en-IN) Globally in WPF App

如何将我的 WPF 应用程序设置为不使用用户计算机的区域性?

最佳答案

这是我在 OnStartup 中使用的。它与我使用 DefaultThreadCurrentCulture 时链接到的 SO 问题不同,因为它为整个进程和启动的任何线程设置默认值。

DefaultThreadCurrentCultureDefaultThreadCurrentUICulture 在 .NET 4.5 及更高版本中......

var newCulture = new CultureInfo(displayCulture);

// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";

// You cannot use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture if
// you dont have .NET 4.5 or later. Just remove these two lines and pass the culture
// to any new threads you create and set Thread.CurrentThread...
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;

Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;

FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

关于c# - WPF 默认日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com