更改UWP应用程序中的当前线程区域性

时间:2019-05-20 16:18:07

标签: c# uwp currentculture

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项目中也可以。

1 个答案:

答案 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();

        }
    }