多语言wpf应用程序与资源在Visual Studio设计器
本文关键字:Studio Visual 资源 语言 wpf 应用程序 | 更新日期: 2023-09-27 18:04:18
这是我的问题:我有多语言WPF应用程序的资源在两个不同的文件。现在我在app. example .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>
那么我在设计器中有这些资源,但是我不能设置多语言。
是否有可能在多语言应用程序的设计器中可见资源?也许某种改变app.xaml文件,而应用程序是打开的?
你走对了路。
-
我建议您在添加新字典之前清除应用程序合并字典。
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);
-
你的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>
-
当你从资源中获得本地化值时,你必须使用DynamicResources而不是StaticResources:
<TextBlock Text="{DynamicResource MyString}" />
这对我有用。