【发布时间】:2013-12-30 00:28:58
【问题描述】:
我的程序的主菜单使用由MenuItems组成的ContextMenu。在我的程序本地化期间(使用资源字典),我将DynamicResource 设置为每个MenuItems 的Header。奇怪的是 DynamicResource 编译,但似乎不会影响本地化期间的任何更改(Headers 上的语言不会改变)。
MenuItem 的示例:
//I'm not sure if the x:Name or the PlacementRectangle is interfering with anything...
<ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
<MenuItem Header="{DynamicResource open}" />
</ContextMenu>
MenuItem 控件的约束是什么?它应该与DynamicResource 一起使用吗?我的总体目标是本地化这些strings,我该怎么做?
这个程序在 WPF 中。谢谢。
更新: 这就是在我的 App.xaml 文件中引用我的资源字典的方式:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Lang.en-US.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Application.Resources>
更新 2: 我的英语资源词典中的示例字符串:
<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="open">Open</sys:String>
</ResourceDictionary>
更新 3: 我如何将当前资源字典更改为西班牙语的示例函数:
private void spanishChange_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
(ResourceDictionary)Application.LoadComponent(new Uri("LangspES.xaml", UriKind.Relative)));
LanguageChange.FireLanguageChanged();
}
【问题讨论】:
-
是的,我看过这个。如果这是唯一的解决方案,我可能需要为我的菜单使用另一种方法。
-
你是如何创建这个菜单项的?它应该可以工作。
-
MenuItem包含在ContextMenu的实现中。我会更新我的问题 -
\Resources\LocalizedResources.resx,Content="{x:Static res:LocalizedResources.YOURKEY}"。这就是我做本地化的方式。正在读取的语言取决于线程区域性设置: // 以下行提供数据格式的本地化 Thread.CurrentThread.CurrentCulture = YOURCULTURE; // 以下行为应用程序的用户界面提供本地化 Thread.CurrentThread.CurrentUICulture = YOURCULTURE;
标签: c# xaml localization menuitem dynamicresource