我找到了另一种本地化枚举的方法:
这是我的课程,您可以以此为例:ru_RU 和 en_US - 资源文件名。
公共类 EnumLocalizationManager : BindableObject { 公共语言语言;私有的 CommonLocalization commonLang; 私有 ObservableCollection rootCoverterTypes;
public EnumLocalizationManager()
{
commonLang = CommonLocalization.GetInstance;
EnumLanguage = commonLang.Lang;
}
//Коллекция для локализации enum RootConverterType
public static Dictionary<Language, ObservableCollection<string>> RootConverterLocalization = new Dictionary<Language, ObservableCollection<string>>()
{
{
Language.ru_RU, new ObservableCollection<string>()
{
ru_RU.CameraEnumConverterTypeUndefined, ru_RU.CameraEnumConverterTypeAuto, ru_RU.CameraEnumConverterTypeNumber, ru_RU.CameraEnumConverterTypeExponent, ru_RU.CameraEnumConverterTypeDecimal, ru_RU.CameraEnumConverterTypeInteger
}
},
{
Language.en_US, new ObservableCollection<string>()
{
en_US.CameraEnumConverterTypeUndefined, en_US.CameraEnumConverterTypeAuto, en_US.CameraEnumConverterTypeNumber, en_US.CameraEnumConverterTypeExponent, en_US.CameraEnumConverterTypeDecimal, en_US.CameraEnumConverterTypeInteger
}
}
};
//Коллекция для локализации enum ConverterType
public static Dictionary<Language, ObservableCollection<string>> ConverterLocalization = new Dictionary<Language, ObservableCollection<string>>()
{
{
Language.ru_RU, new ObservableCollection<string>()
{
ru_RU.CameraEnumConverterTypeAuto, ru_RU.CameraEnumConverterTypeNumber, ru_RU.CameraEnumConverterTypeExponent, ru_RU.CameraEnumConverterTypeDecimal, ru_RU.CameraEnumConverterTypeInteger
}
},
{
Language.en_US, new ObservableCollection<string>()
{
en_US.CameraEnumConverterTypeAuto, en_US.CameraEnumConverterTypeNumber, en_US.CameraEnumConverterTypeExponent, en_US.CameraEnumConverterTypeDecimal, en_US.CameraEnumConverterTypeInteger
}
}
};
public ObservableCollection<string> RootConverterTypes
{
get { return rootCoverterTypes; }
}
public ObservableCollection<string> ConverterTypes
{
get { return coverterTypes; }
}
public Language EnumLanguage
{
get { return language; }
set
{
language = value;
ChangeEnumLanguage();
}
}
private void ChangeEnumLanguage()
{
if (RootConverterLocalization.ContainsKey(language))
{
rootCoverterTypes = RootConverterLocalization[language];
}
if (ConverterLocalization.ContainsKey(language))
{
coverterTypes = ConverterLocalization[language];
}
RaisePropertyChanged();
RaisePropertyChangedByName("RootConverterTypes");
RaisePropertyChangedByName("ConverterTypes");
}
}
}
BindableObject 类是封装了 INotifyPropertyChanged 的类。首先 - 您的枚举必须编号(ValueConverter 需要)例如:public enum ConverterType { Auto = 0, Number = 1, Exponential = 2, Decimal = 3, Integer = 4 }
public enum RootConverterType
{
Undefined = 0,
Auto = 1,
Number = 2,
Exponential = 3,
Decimal = 4,
Integer = 5
}
最后一部分 - ValueConvert 本身:
类 EnumCameraVariantToLocalizedStringConverter:ConverterBase { public EnumCameraVariantToLocalizedStringConverter() {
}
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)(CameraVariant)value;
}
public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int index = (int)value;
switch (index)
{
case 0:
return CameraVariant.Undefined;
case 1:
return CameraVariant.FirstPerson;
case 2:
return CameraVariant.ThirdPerson;
case 3:
return CameraVariant.Flight;
}
return index;
}
}
我使用从基类继承只是为了使用 makrup 扩展而不为每个转换器添加资源。
和绑定本身:
<ComboBox Style="{StaticResource CameraMainSelectorStyle}"
ItemsSource="{Binding Source={StaticResource EnumLocalizationManager}, Path=CameraVariant}"
SelectedIndex="{Binding Path=CameraSettingsManager.StartUpCameraModeFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={valueConverters:EnumCameraVariantToLocalizedStringConverter}}"
Tag="{Binding Path=CameraSettingsManager.StartUpCameraModeFilter, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="StartUpCameraTypeFilter_OnSelectionChanged"/>
这是将枚举绑定到 Combobox。我希望一切都清楚。这里有一件事。如果要即时更改语言,则必须添加一些代码以在更改语言后不丢失所选项目:
if (((ComboBox)sender).SelectedIndex < 0)
{
if (((ComboBox) sender).Tag != null)
{
CameraVariant behavior = (CameraVariant) ((ComboBox) sender).Tag;
((ComboBox) sender).SelectedIndex = (int) behavior;
}
}
就这些。看起来有点吓人,但没有什么难的。