在UWP
应用程序中,我想像这样更改新创建的线程的区域性:
private void btnRun_Click(object sender, RoutedEventArgs e)
{
newThread = new Thread(new ThreadStart(TestMethod));
newThread.Start();
}
private void TestMethod()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
}
在线程方法主体中设置CurrentCulture
属性会导致所有应用程序线程区域性(以及UI
线程)都发生更改。
仅在UWP
应用程序中会出现此问题,在其他类型的.net项目中也可以。
答案 0 :(得分:0)
我在Xamarin.Forms中使用UWP,所以我的解决方法如下:
在App.xaml.cs的OnLauncehd方法内部,我编写了以下代码:
CultureInfo ci = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
ApplicationLanguages.PrimaryLanguageOverride = "en-GB";
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
如果它不起作用,我也可以在MainPage.xaml.cs中这样做:
public sealed partial class MainPage
{
public MainPage()
{
this.Loaded += (sender, args) => {
SetEnGbCulture();
};
}
public void SetEnGbCulture()
{
CultureInfo ci = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
ApplicationLanguages.PrimaryLanguageOverride = "en-GB";
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
}
}