c# - 如何为整个 Winform 应用程序设置文化
问题描述
我想为整个 winform 应用程序设置一种文化。
我怎样才能做到这一点?
我Program.cs
像这样更改了我的文件:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Divar
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var culture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new RadForm1());
}
}
}
我做对了吗?
还有另一个链接:(请看一下)
https://www.c-sharpcorner.com/forums/set-cultureinfo-for-winform-application
这取得了有限的成功,所以在我放置的 InitializeComponent() 之前的“Form1”顶部:
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-GB");
Application.CurrentCulture = cultureInfo;
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
是否有必要在每个表单的 InitializeComponent() 之前添加这三行?
解决方案
将这两个设置Main
为所需的文化:
CultureInfo.DefaultThreadCurrentCulture
CultureInfo.DefaultThreadCurrentUICulture
此外,您可以随时更改Application.CurrentCulture
应用程序当前线程上的区域性。
[STAThread]
static void Main()
{
var culture = CultureInfo.GetCultureInfo("en-US");
// this may fail sometimes: (see Drachenkatze's comment below)
// var culture = new CultureInfo("en-US");
//Culture for any thread
CultureInfo.DefaultThreadCurrentCulture = culture;
//Culture for UI in any thread
CultureInfo.DefaultThreadCurrentUICulture = culture;
//Culture for current thread (STA)
//no need for: Application.CurrentCulture = culture;
//Thread.CurrentThread.CurrentCulture == Application.CurrentCulture
//no need for: Thread.CurrentThread.CurrentCulture = culture;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new RadForm1());
}
推荐阅读
- vba - 如何添加 HTMLBody 签名?
- python - 我怎样才能在不同的位置产卵?
- java - 日期、月份和/或年份未知的 Java 日期实现
- python - 将两个 np.random.normal 分布数组与 for 循环相乘
- wordpress - 如何计算与另一个分类相匹配的自定义分类的术语数
- azure - 逻辑应用自定义连接器正在替换位置标头
- javascript - Svg 未在单击/加载页面上呈现
- postgresql - SOA 数据复制和架构
- git - 无法在 Visual Studio Code 中克隆/更新私有 GitHub 存储库
- rust - 使用 split_mut 或类似方法对切片的不相交部分进行多个可变引用