1

我有一个需要支持本地化的 WPF 应用程序。我有 2 个按钮(添加、删除)和一个组合框(英语、法语)。当我选择法语时,按钮的内容应更改为 (ajouter, effacer)。

现在,我的问题是,如果我在后面的代码中手动设置按钮内容,会有什么缺点或缺点?请参阅下面的代码。

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  ResourceDictionary currentLanguage = new ResourceDictionary();

  switch (comboBox.Text)
  {
    case "english": 
      currentLanguage.Source = new Uri("../en-US.xaml", UriKind.RelativeOrAbsolute);
      break;
    case "france": 
      currentLanguage.Source = new Uri("../fr-FR.xaml", UriKind.RelativeOrAbsolute);
      break;
  }

  btnAdd.Content = Resources.GetString("insert");
  btnDelete.Content = Resources.GetString("delete");
}

我知道这有点奇怪,但请回答。为什么我不应该以这种方式本地化我的应用程序。

谢谢!

4

1 回答 1

3

在交换资源字典时,您应该在 XAML 中使用 usingDynamicResources而不是StaticResourcesBasicly,您将执行以下操作:

XAML

  <Button Content="{DynamicResource Insert}"/>
  <Button Content="{DynamicResource Delete}"/>

使用 x:Key 在资源字典中插入和删除的位置

当您交换字典时,这些将动态更新

于 2012-06-06T14:16:20.150 回答