长时间潜伏,第一次海报 . 我会说'温柔',但我知道的更好 . 我希望我的第一篇文章更容易或更简单,但如果我这样做,它已经在某个地方得到了解答 .
我正在研究各个部分已经讨论过的问题,但是我很难将它们放在一起 . 我们有一个应用程序,有很多插件需要不同的输入参数,我正在尝试多语言 . 我一直在研究动态GUI,它检查插件以创建输入参数数组,并使用DataTemplateSelector根据参数的类型选择正确的控件 . 对于枚举器,我们尝试将本地化的显示名称绑定到组合框 . StackOverflow上有很多关于如何进行枚举/组合框绑定的线程,但我找不到多语言和动态(datatemplate或其他) .
Brian Lagunas有一篇很棒的博客文章,几乎让我们在那里:http://brianlagunas.com/localize-enum-descriptions-in-wpf . 然而,他静静地绑定了XAML中的枚举 . 我们有数百个枚举,并且一直在创造新的枚举 . 所以我输入并将其绑定到组合框,但我无法确定在何处,何时或如何 .
我在这里上传了一个扩展示例:https://github.com/bryandam/Combo_Enum_MultiLingual . 我很难将它浓缩下来 .
public partial class MainWindow : Window
{
public ObservableCollection<Object> InputParameterList { get; set; } = new ObservableCollection<Object>();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
//Create an example input object.
InputParameter bitlocker_drive = new InputParameter();
bitlocker_drive.Name = "BitLocker Enabled";
bitlocker_drive.Type = typeof(String);
InputParameterList.Add(bitlocker_drive);
InputParameter bitlocker_status = new InputParameter();
bitlocker_status.Name = "Status";
bitlocker_status.Type = typeof(Status);
InputParameterList.Add(bitlocker_status);
InputParameter bitlocker_foo = new InputParameter();
bitlocker_foo.Name = "Foo";
bitlocker_foo.Type = typeof(Foo);
InputParameterList.Add(bitlocker_foo);
}
}
这是我的XAML:
<Window x:Class="BindingEnums.MainWindow"
....
<Window.Resources>
...
<DataTemplate x:Key="ComboBox">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name, Mode=TwoWay}" />
<ComboBox ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}" Grid.Column="1"/>
</Grid>
</DataTemplate>
...
<local:InputParameterTemplateSelector x:Key="InputDataTemplateSelector" Checkbox="{StaticResource Checkbox}" ComboBox="{StaticResource ComboBox}" DatePicker="{StaticResource DatePicker}" TextBox="{StaticResource TextBox}"/>
</Window.Resources>
<Grid>
<ListBox Name="InputParameters" KeyboardNavigation.TabNavigation="Continue" HorizontalContentAlignment="Stretch" ItemsSource="{Binding InputParameterList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="Transparent" BorderBrush="Transparent" ItemTemplateSelector="{StaticResource InputDataTemplateSelector}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
这是我正在测试的两个示例枚举:
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Status
{
[Display(Name = nameof(Resources.EnumResources.Good), ResourceType = typeof(Resources.EnumResources))]
Good,
[Display(Name = nameof(Resources.EnumResources.Better), ResourceType = typeof(Resources.EnumResources))]
Better,
Best
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Foo
{
[Display(Name = nameof(Resources.EnumResources.Foo), ResourceType = typeof(Resources.EnumResources))]
Foo,
[Display(Name = nameof(Resources.EnumResources.Bar), ResourceType = typeof(Resources.EnumResources))]
Bar
}
这是枚举类型转换器:
public class EnumDescriptionTypeConverter : EnumConverter
{
public EnumDescriptionTypeConverter(Type type)
: base(type)
{}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
//Reflect into the value's type to get the display attributes.
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DisplayAttribute displayAttribute = fieldInfo?
.GetCustomAttributes(false)
.OfType<DisplayAttribute>()
.SingleOrDefault();
if (displayAttribute == null)
{
return value.ToString();
}
else
{
//Look up the localized string.
ResourceManager resourceManager = new ResourceManager(displayAttribute.ResourceType);
string name = resourceManager.GetString(displayAttribute.Name);
return string.IsNullOrWhiteSpace(name) ? displayAttribute.Name : name;
}
}
}
return string.Empty;
}
return base.ConvertTo(context, culture, value, destinationType);
}
这是Enum绑定源标记扩展:
public class EnumBindingSourceExtension : MarkupExtension
{
...
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
同样,我的目标是弄清楚如何避免静态绑定到单个枚举类型(如下面的XAML中),而是根据输入参数的类型绑定它:
<ComboBox ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}" Grid.Column="1"/
我已经在Window代码隐藏,数据模板选择器,甚至自定义控件中完成了它,但没有太大的成功 . 我的第一个“真正的”WPF应用程序,所以我无可否认地将我所有这些与他们各自的部分放在一起 .
1 回答
对此的一个可能的“解决方案”是完全放弃使用枚举进行本地化 . 这就是我所做的,它允许我自己和项目中的其他开发人员将简单的英语插入到我们的XAML中,如下所示:
我编写了一个小实用程序来扫描我们的XAML文件,并将这些实例拉到Excel电子表格中,然后发送给翻译人员,第二个实用程序将我们收到的翻译文件打包并将其打包成XML文件(每种语言一个) . 这些文件基本上是字典,其中XAML中的英文文本用作查找当前所选语言的翻译的关键字:
这有很多好处:
开发人员仍然用一种通用语言编写他们的XAML(在我的例子中是英语) .
每次向前端添加新文本时,都不必重建解决方案中的每个项目 .
如果添加没有't been translated yet then the `Translate'扩展名的新文本,请回到英文翻译 .
XML文件存储在本地,因此客户可以随意更改本地化文本(包括英语) .
您可以完全控制GUI中的哪些字段进行翻译,哪些字段不进行翻译 .
当然,如果您在运行时更改转换,则正在翻译的控件将自行更新并立即切换到新语言 .
显然,这个系统的关键是编写'Translate'自定义标记扩展,幸运的是有人已经为你完成了这个:
https://www.wpftutorial.net/LocalizeMarkupExtension.html