Bol*_*lta 2 c# enums xaml windows-phone-8 windows-phone-8.1
我有一个枚举
public enum AccountType
{
Cash,
PrepaidCard,
CreditCard,
Project
}
Run Code Online (Sandbox Code Playgroud)
这是ItemsSource的代码
typeComboxBox.ItemsSource = Enum.GetValues(typeof(AccountType)).Cast<AccountType>();
Run Code Online (Sandbox Code Playgroud)
我希望用多语言转换器将它绑定到我的ComboBox


我怎样才能做到这一点?
我没有在本地化方面做过很多工作,但我可能会使用自定义转换器来解决这个问题.(当你使用ComboBox我假设你正在做一个Windows Phone Store应用程序,而不是Windows Phone Silverlight应用程序).
1:将枚举值的翻译添加到不同的Resources.resw文件中(例如,/Strings/en-US/Resources.resw对于美国英语,请参阅http://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa),该表格如下所示:
|-------------|--------------|--------------|
| Name | Value | Comment |
|-------------|--------------|--------------|
| Cash | Cash | |
| PrepaidCard | Prepaid card | |
| CreditCard | Credit card | |
| Project | Project | |
Run Code Online (Sandbox Code Playgroud)
2:然后创建一个自定义转换器:
public class LocalizationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return ResourceLoader.GetForCurrentView().GetString(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
3:将其添加到资源Dictionary中App.xaml,例如:
<Application.Resources>
<local:LocalizationConverter x:Key="LocalizationConverter" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
4:在ComboBox,创建一个使用此转换器的项模板:
<ComboBox x:Name="typeComboxBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource LocalizationConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
可能有一种更简单的方法,但这是我现在能想到的最好的方法.
| 归档时间: |
|
| 查看次数: |
2064 次 |
| 最近记录: |