【问题标题】:How to get designtime text visible in a multi-language application in WPF?如何在 WPF 的多语言应用程序中显示设计时文本?
【发布时间】:2016-11-10 13:32:46
【问题描述】:

我有一个 WPF 应用程序,我在其中使用资源文件使其成为多语言。

像这样:

private void Application_Startup(object sender, StartupEventArgs e)
{
    try
    {
        var lang = new Uri("pack://siteoforigin:,,,/Languages/" + Settings.Default.Language + ".xaml", UriKind.RelativeOrAbsolute);
        this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = lang });
    }
    catch
    {
        this.Resources.MergedDictionaries.Clear();
        this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("..\\Languages\\English.xaml", UriKind.RelativeOrAbsolute) });
    }
}

因此,对于任何显示文本的标签,我将它们设置为:

<TextBlock Text="{DynamicResource DYouKnow}"/>

这在运行时效果很好,但在设计时我在标签上看不到默认文本,这使得设计/维护 UI 变得很困难。

还有其他方法吗?

【问题讨论】:

  • 欢迎来到 SO!我编辑了您的问题并删除了代码 sn-ps 标签,因为您提供了 c# 代码示例而不是 html 代码。

标签: c# wpf xaml localization


【解决方案1】:

使用 MarkupExtension,您可以创建一个可在 XAML 中使用的扩展,该扩展也将提供设计时文本。

您可以按如下方式使用它:

<TextBlock loc:TranslateExtension.Key="txtTextKey" Text="{loc:TranslateExtension Default='Default text'}" />

请参阅以下教程: http://www.wpftutorial.net/localizemarkupextension.html

【讨论】:

    【解决方案2】:

    InitializeComponent() 调用之后在您的 View 构造函数中尝试以下操作:

    if (DesignerProperties.GetIsInDesignMode(this))
         this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("..\\Languages\\English.xaml", UriKind.RelativeOrAbsolute) })
    

    ..将 XAML URI 调整为当前视图文件的特定文件夹位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 2012-02-22
      • 1970-01-01
      • 2022-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多