2013-12-11 40 views
6

我的程序主菜單使用ContextMenu組成MenuItems。在我的程序本地化過程中(使用資源字典),我將DynamicResource設置爲我的MenuItems中的每一個的Header。奇怪的是DynamicResource編譯,但似乎不影響本地化過程中的任何更改(Headers上的語言不會更改)。爲什麼MenuItem不能使用DynamicResource?

的實施例一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中。謝謝。

UPDATE: 這是我的資源字典是如何在我的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: 例子f結對我如何改變目前的資源字典西班牙:

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(); 
} 
+0

你看着微軟的建議? http://msdn.microsoft.com/en-us/library/ms788718(v=vs.110).aspx –

+0

是的,我見過這個。如果這是唯一的解決方案,它看起來像我可能需要使用另一種方法爲我的菜單。 –

+0

你是如何創建這個MenuItem的?它應該工作。 –

回答

2

你有沒有加入LANGUAGE.xaml文件到App.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應該可以工作,這讓我想知道你在你的應用程序中正確設置了本地化嗎?

4.5如何設置本地化.NET看到this msdn link

+0

我正在引用我的字典就像這樣 –

+0

這些方法的非工作.. 。: - /現在我甚至用'TextBlock'嘗試它。我加倍檢查,這些詞絕對在我的資源字典中。我可以驗證本地化是否正確設置,因爲我的程序的其餘部分本地化很好。 –

+0

嗯......這不是愚蠢的,'開放'是一個保留字嗎?嘗試改變其他東西只是包裝(值得嘗試)。 –

相關問題