C# 在WPF应用程序中单击按钮可重置区域性
我在WPF应用程序中看到了奇怪的行为(我确实在一个示例应用程序中复制了它) 当应用程序启动时,在加载主应用程序窗口之前,我将CultureInfo.CurrentCulture和CultureInfo.CurrentUICulture设置为例如法语。然后加载主应用程序窗口并按预期显示本地化文本(法语),如下所示:C# 在WPF应用程序中单击按钮可重置区域性,c#,wpf,localization,cultureinfo,C#,Wpf,Localization,Cultureinfo,我在WPF应用程序中看到了奇怪的行为(我确实在一个示例应用程序中复制了它) 当应用程序启动时,在加载主应用程序窗口之前,我将CultureInfo.CurrentCulture和CultureInfo.CurrentUICulture设置为例如法语。然后加载主应用程序窗口并按预期显示本地化文本(法语),如下所示: <Window.Resources> <Strings x:Key="Resources"/> </Window.Resources <But
<Window.Resources>
<Strings x:Key="Resources"/>
</Window.Resources
<Button Content={Binding Label, Source={StaticResource Resources}}/>
及
在主窗口加载之前,区域性仍然会被重置,但这没有帮助
更新:我在WPF论坛()上也问了这个问题。在这里,我描述的实际场景可能更好一些:我需要能够在应用程序启动时设置特定的区域性,并在应用程序的整个生命周期中“保留”。不幸的是,单击按钮时,区域性会重置,如所述 可能与您的窗口有关。资源->显示资源字符串的绑定方法。我显示如下本地化字符串:
<Window x:Class="CultureTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:CultureTest.Properties"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="{x:Static p:Resources.Greeting}"/>
</Grid>
它保持了良好的文化。这是您的选择吗?这个问题可能有您需要的答案。由于某种原因,区域性似乎未应用于控件。不过,我不确定你是否能做得更多。有趣的是,如果我在按钮的单击处理程序中设置一个断点,则只有在第一次点击时,区域性才会重置为en US。如果单击一次后,我再次设置了法语区域性,然后再次单击按钮,则CurrentCulture和CurrentUICulture都是法语。不幸的是,这不是一个选项,因为我需要能够在运行时动态更改区域性(用户从列表中选择),并且在这种情况下使用x:Static扩展不起作用(按钮的内容不变)
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
<Window x:Class="CultureTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:CultureTest.Properties"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="{x:Static p:Resources.Greeting}"/>
</Grid>
protected override void OnStartup(StartupEventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");
base.OnStartup(e);
}