我有一個WPF應用程序,我支持多種語言。我可以更改語言運行時,因爲我在語言更改時加載包含字符串資源的不同資源字典集。如何在更改語言時更新由轉換器生成的字符串?
private void UpdateLoadedLanguages(object sender, SelectionChangedEventArgs e)
{
RemoveLoadedLanguages();
ResourceDictionary aResourceDictionary = new ResourceDictionary();
ResourceDictionary bResourceDictionary = new ResourceDictionary();
ResourceDictionary cLanguageResourceDictionary = new ResourceDictionary();
Language selectedLanguage = (Language) e.AddedItems[0];
switch (selectedLanguage)
{
case Language.enUS:
a.Source = new Uri("pack://application:,,,/[Projecta];component/src/Helpers/Language.en-US.xaml");
b.Source = new Uri("pack://application:,,,/[Projectb];component/src/resources/Language.en-US.xaml");
c.Source = new Uri("pack://application:,,,/[Projectc];component/src/resources/Language.en-US.xaml");
break;
case Language.nlNL:
a.Source = new Uri("pack://application:,,,/[Projecta];component/src/Helpers/Language.nl-NL.xaml");
b.Source = new Uri("pack://application:,,,/[Projectb];component/src/resources/Language.nl-NL.xaml");
c.Source = new Uri("pack://application:,,,/[Projectc];component/src/resources/Language.nl-NL.xaml");
break;
default:
a.Source = new Uri("pack://application:,,,/[Projecta];component/src/Helpers/Language.en-US.xaml");
b.Source = new Uri("pack://application:,,,/[Projectb];component/src/resources/Language.en-US.xaml");
c.Source = new Uri("pack://application:,,,/[Projectc];component/src/resources/Language.en-US.xaml");
break;
}
App.Current.Resources.MergedDictionaries.Add(aResourceDictionary);
App.Current.Resources.MergedDictionaries.Add(bResourceDictionary);
App.Current.Resources.MergedDictionaries.Add(cResourceDictionary);
}
與翻譯資源字典通常是這樣的:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="DefaultApplicationTitleString">Title</system:String>
<system:String x:Key="DefaultDoneString">Done</system:String>
<system:String x:Key="DefaultCancelString">Cancel</system:String>
<system:String x:Key="DefaultDescriptionString">Description unknown</system:String>
<system:String x:Key="LanguageSelectionString">Language</system:String>
<system:String x:Key="AisleString">Aisle</system:String>
<system:String x:Key="LevelsString">levels</system:String>
</ResourceDictionary>
當我使用像上面這樣定義的正下方運行完美的字符串之一。
Title="{DynamicResource DefaultApplicationTitleString}"
但隨着這樣的轉換器...
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = (string) Application.Current.FindResource("DefaultDescriptionString");
if (value != null)
{
string temp = (string) value;
char[] stringSeparators = { 'A', '.', '-' };
string[] stringElements= temp.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
string aisleString = (string) Application.Current.FindResource("AisleString");
string levelsString = (string) Application.Current.FindResource("LevelsString");
text = aisleString + " " + stringElements[1] + ", " + levelsString + " " + stringElements[2] + " - " +
stringElements[3];
}
return text;
}
...我用這樣的...
<TextBlock Text="{Binding Id, Converter={StaticResource ItemIdToTextConverter}}" />
...然後我的字符串不當我更改語言時更新。如果我轉換的屬性在其viewModel上更新,那麼語言改變就會發生。
所以我的問題:有沒有人有我的解決方案,我的字符串作爲轉換器的結果更新語言的變化?
我一個解決方案的猜測是:(?我將如何做到這一點)
- 的力量,使用資源的所有frameworkelements的更新
- 或可能做一些與DynamicResourceExtenstion(我發現這個問題:?Return a dynamic resource from a converter,但我不明白的DynamicResourceExtension的解釋所以這是要走的路如果是的話,我將如何實現這一點)
謝謝您的回覆!我從第一種解決問題的方式中獲得靈感,致電屬性改變了事件。所以我用一個方法擴展了viewmodel的基類,該方法引發了與該viewModel相關的所有屬性的屬性更改事件。在改變語言選擇時,我爲所有加載的viewModel調用該方法。 – cherryorange 2014-10-21 08:57:03