从人口枚举一个ListPicker使用本地化的字符串 [英] Using localized strings in a ListPicker populated from Enum

查看:142
本文介绍了从人口枚举一个ListPicker使用本地化的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充 ListPicker 枚举。举例来说,如果我有以下枚举:

I'm populating a ListPicker from an Enum. For example, if I have the following Enum:

public enum Pets 
{
    Dog,
    Cat,
    Platypus 
}

我填充ListPicker在以下方式:

I populate the ListPicker in the following way:

PetListPicker.ItemsSource = Enum.GetValues(typeof(Pets));



一切OK,直到有。我ListPicker控件显示项目选择的名称。

Everything OK until there. My ListPicker control shows the names of items to choose.

的问题是,我要本地化该枚举项目在不同的语言中使用它们。也就是说,我想的ListPicker以显示该应用程序正在使用的语言的名称。

The problem is that I want to localize that Enum items to use them in different languages. That is, I'd like the ListPicker to show the names in the language that the app is currently using.

我在资源文件,我用本地化的应用程序的其余部分本地化字符串。但是,我不知道如何使它与ListPicker项目工作。

I have the localizes strings in resource files, which I use to localize the rest of the app. However, I don't know how to make it to work with the ListPicker items.

推荐答案

我终于找到了一种方法,使用实现我的目标说明属性,枚举值和转换

I have finally found an approach to achieve my goal using Description attribute for enum values and a Converter.

既然是无法使用的值从资源direcatly文件作为说明的属性,首先我创建我的自定义LocalizedDescriptionAttribute类,它DescriptionAttribute继承:

Since it is not possible to use the values from the Resources file direcatly as Description attributes, first of all I created my custom LocalizedDescriptionAttribute class, which inherits from DescriptionAttribute:

public class LocalizedDescriptionAttribute : DescriptionAttribute
{
    public LocalizedDescriptionAttribute(string resourceId)
        : base(GetStringFromResource(resourceId))
    { }

    private static string GetStringFromResource(string resourceId)
    {
        return AppResources.ResourceManager.GetString(resourceId);
    }
}



这样我可以使用资源的ID作为LocalizedDescription属性:

That way I can use the ID of the resource as LocalizedDescription attribute:

public enum Pet
{
    [LocalizedDescription("Dog")]
    Dog,
    [LocalizedDescription("Cat")]
    Cat,
    [LocalizedDescription("Platypus")]
    Platypus 
}

在这里,我创建了一个 ValueConverter 这确实shwoing字符串的工作在我ListPicker适当的语言:

Once here, I created a ValueConverter which does the work of shwoing the string in the appropriate language in my ListPicker:

public class EnumToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    DescriptionAttribute attr =
                           Attribute.GetCustomAttribute(field,
                             typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

完成这一点,我创建了一个的DataTemplate 为ListPicker项目,由绑定,并使用转换器设置TextBlock的Text属性的值

Done this, I created a DataTemplate for the ListPicker items, setting the value of the Text property of a TextBlock by a Binding and using the Converter:

<DataTemplate x:Key="ListPickerDataTemplate">
    <Grid>
        <TextBlock Text="{Binding Converter={StaticResource EnumToStringConverter}}"/>
    </Grid>
</DataTemplate>

和我填充ListPicker以同样的方式我以前做过:

And I populate the ListPicker the same way I did it before:

PetListPicker.ItemsSource = Enum.GetValues(typeof(Pet));

现在我ListPicker显示项目的本地化值,其优点是在 SelectecItem 的ListPicker的属性可以绑定到枚举类型的属性。

Now my ListPicker shows the localized values of the items, with the advantage that the SelectecItem property of the ListPicker can be binded to a property of the type of the Enum.

例如,如果我有以下属性我的视图模型,在这里我要存储选择的项目:

For example, if I have following property in my ViewModel, where I want to store the selected item:

public Pet MyPet {get; set;};



我可以只使用一个绑定:

I can just use a binding:

<toolkit:ListPicker x:Name="MyListPicker" SelectedItem="{Binding MyPet, Mode=TwoWay}" ItemTemplate="{StaticResource ListPickerDataTemplate}"/>

这篇关于从人口枚举一个ListPicker使用本地化的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆