繁体   English   中英

具有Visual Studio Designer中资源的多语言WPF应用程序

[英]Multilanguage wpf application with resources in Visual Studio Designer

这是我的问题:我有多语言WPF应用程序,资源在两个不同的文件中。 现在,我像这样在app.xaml.cs中选择合适的一个:

var dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
    case "de-DE":
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
        break;
    default:
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
        break;
}
Resources.MergedDictionaries.Add(dict);

一切正常,但我在VisualStudio Designer中看不到该资源。

另一方面,当我在App.xaml文件中定义ResourceDictionary时,如下所示:

<Application x:Class="Ampe.UI.Views.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

然后我在Designer中拥有此资源,但是我无法设置多语言。

使用多语言应用程序的设计器中的可见资源是否有可能? 也许在打开应用程序时更改了app.xaml文件?

您的方法正确。

  1. 我建议您在添加新字典之前先清除应用程序合并的字典。

      Resources.MergedDictionaries.Clear(); var dict = new ResourceDictionary(); switch (Thread.CurrentThread.CurrentCulture.ToString()) { case "de-DE": dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute); break; default: dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute); break; } Resources.MergedDictionaries.Add(dict); 
  2. 您的app.xaml应该看起来像您所说的:

     <Application x:Class="Ampe.UI.Views.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Exit="App_OnExit" ShutdownMode="OnMainWindowClose"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 
  3. 从资源获取本地化的值时,必须使用DynamicResources而不是StaticResources:

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

这个对我有用。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM