【问题标题】:Why don't MenuItems work with DynamicResource?为什么 MenuItems 不能与 DynamicResource 一起使用?
【发布时间】:2013-12-30 00:28:58
【问题描述】:

我的程序的主菜单使用由MenuItems组成的ContextMenu。在我的程序本地化期间(使用资源字典),我将DynamicResource 设置为每个MenuItemsHeader。奇怪的是 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


【解决方案1】:

您是否将 LANGUAGE.xaml 文件添加到 App.ResourceDictionary 或控制 ResourceDictionary?

例如

<Application.Resources>
    <ResourceDictionary Source="LANGUAGE1.xaml" />
    <ResourceDictionary Source="LANGUAGE2.xaml" />
</Application.Resources>

如果不是,您是如何引用资源字典的?

更新:

如果你改变了

<MenuItem Header="{DynamicResource open}" />

<MenuItem Header="{StaticResource open}" />

然后它工作吗?甚至可以

<TextBox DockPanel.Dock="Top" Text="{StaticResource open}" />

工作?

看起来您的 xaml 应该可以工作,这让我想知道您是否在应用中正确设置了本地化?

有关如何在 .net 4.5 中设置本地化,请参阅 this msdn link

【讨论】:

  • 我像这样引用我的字典
  • 这些方法都不起作用... :-/ 现在我什至正在尝试使用TextBlock。我仔细检查了这些词,这些词肯定在我的资源词典中。我可以验证本地化设置是否正确,因为我的程序的其余部分本地化很好。
  • hmmm... 'open' 是保留字不是什么愚蠢的事情吗?试着换成别的东西就装起来(值得一试)。
  • 不行,改了还是什么都没有
  • 把你的语言文件 (xaml) 放在你的问题中,也许里面有东西。
猜你喜欢
  • 2013-07-02
  • 2011-06-15
  • 2018-03-09
  • 2021-06-14
  • 2012-10-09
  • 2020-03-18
  • 2017-11-21
  • 2019-04-11
  • 2012-09-19
相关资源
最近更新 更多