【问题标题】:Content of button is not updated after changing the CurrentUICulture更改 CurrentUICulture 后按钮的内容未更新
【发布时间】:2020-08-25 13:23:08
【问题描述】:

根据this tutorial,我尝试在我的 WPF .NET Core 3.1 应用程序中实现多语言系统。如果我直接更改Window 元素的属性Title,一切正常,应用程序从正确的资源文件中读取,该文件是通过更改CurrentUICulture 定义的。

这里是这样一个改变的代码:

private void BtnChangeLanguageToCsCz_Click(object sender, RoutedEventArgs e)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("cs-CZ");
}

private void BtnChangeLanguageToEnUs_Click(object sender, RoutedEventArgs e)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
}

但是,当我点击这些按钮中的任何一个后,它们的内容仍未更新。我觉得我需要调用一些 UI 更新函数或类似的东西,但到目前为止找不到任何相关内容。

这里是我如何绑定ButtonContent属性:

<Button x:Name="BtnInstall" Content="{x:Static p:Resource.ButtonInstall}" HorizontalAlignment="Center" Margin="0,324,0,0" VerticalAlignment="Top" Height="50" Width="200" Click="BtnInstall_Click"/>

资源文件的修饰符设置为公共,名称如下:

我应该怎么做才能让它自己更新?

【问题讨论】:

  • 有后缀的文件没有*.Designer.cs文件,因为一个Resources类就足够了:它根据应用程序当前的文化从相应的.resx文件中加载资源。您的 .resx 文件应包含 xml 格式的翻译,例如&lt;data name="LongWheelName" xml:space="preserve"&gt;&lt;value&gt;Long Wheel Name Translation&lt;/value&gt;&lt;/data&gt;
  • @ASh 谢谢你的教程,它真的很有帮助。我已经重写了问题。
  • 确保其他资源文件的Build Action是“Embedded Resource”
  • @ReeceRussell 所有资源文件都是Embedded ResourceCopy to Output Directory的值是Do not copy,我应该改一下吗?

标签: wpf .net-core multilingual resx .net-core-3.1


【解决方案1】:

A 找到了基于this 教程的解决方案,而不是使用线程的文化或 resource.resx 文件,而是使用 XAML 资源字典。

这可以根据您的情况进行更改,但是,我为 MainWindow.xaml 执行了此操作:

  • 我在名为 /Resources 的文件夹中创建了两个资源字典,名为“MainWindow.en-GB.xaml”和“MainWindow.cs-CZ.xaml”。
  • 然后我将构建操作设置为Content,将复制到输出目录设置为Copy if newer
  • 创建资源字典后,我创建了一个名为 Text 的示例资源:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:String x:Key="Text">Hello</sys:String>
</ResourceDictionary>
  • 然后在我的 MainWindow.xaml 中,我添加了以下代码:
<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/MainWindow.en-GB.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
  • 接下来,将按钮的内容设置为资源Text
<Button x:Name="BtnInstall" 
    Content="{DynamicResource ResourceKey=Text}" 
    HorizontalAlignment="Center" 
    Margin="0,324,0,0" 
    VerticalAlignment="Top" 
    Height="50" 
    Width="200" 
    Click="BtnInstall_Click" />
  • 最后,通过使用linked tutorial中的LocUtil,调用LocUtil.SwitchLanguage方法,我可以在两者之间进行切换:
private void BtnChangeLanguageToEnUs_Click(object sender, RoutedEventArgs e)
{
    LocUtil.SwitchLanguage(this, "en-US");
}

【讨论】:

  • 我在将&lt;ResourceDisctionary&gt; 部分添加到MainWindow.xaml 时遇到错误,即使没有这部分代码,应用程序也能正常工作。
  • 是的,我找不到任何问题。
  • 完美,希望一切顺利!
  • 我一直认为上传图片比插入代码块更费力,更不方便。但人们总是这样做。我想知道为什么?
  • @Ash 我同意,不太方便;但是,我在使用带有 XML/XAML 的代码编辑器时遇到了问题,它要么无法正确显示,要么根本无法显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多