代码之家  ›  专栏  ›  技术社区  ›  Justin XL

是否有任何方法可以在XAML(Silverlight)中创建字符串的动态列表(基于语言)?

  •  0
  • Justin XL  · 技术社区  · 15 年前

    只是想知道是否可以基于语言/区域性在XAML中动态创建字符串列表?假设用户以英语用户身份登录,则显示客户名称、订单号…如果用户以波兰用户的身份登录,会显示nazwa klienta,numer zamwienia?

    我只知道下面的硬编码:

            <System_Collections_Generic:List`1 x:Key="columnNameList">
                <System:String>Client Name</System:String>
                <System:String>Order Number</System:String>
                <System:String>Date</System:String>
            </System_Collections_Generic:List`1>
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Thomas Levesque    15 年前

    我建议使用资源文件和标记扩展名。在资源文件中,创建字符串资源,并为每种语言生成本地化的资源文件。在标记扩展中,您只需从资源返回字符串的值(它将自动从附属资源程序集中为当前区域性选择适当的语言)。

    标记扩展

    [MarkupExtensionReturnType(typeof(string))]
    public class ResourceString : MarkupExtension
    {
        [ConstructorArgument("resourceKey")]
        public string ResourceKey { get; set; }
    
        public ResourceString(string resourceKey)
        {
            this.ResourceKey = resourceKey;
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            // Assuming your resource file is named StringResources.resx
            return StringResources.ResourceManager.GetString(ResourceKey);
        }
    }
    

    XAML

        <local:ListOfStrings x:Key="columnNameList">
            <local:ResourceString ResourceKey="ClientName" />
            <local:ResourceString ResourceKey="OrderNumber" />
            <local:ResourceString ResourceKey="Date" />
        </local:ListOfStrings>
    

    顺便说一下,您不能在XAML中使用泛型(好吧,您可以在XAML 2009中使用,但在VS中还不支持它)。因此,您需要创建一个表示字符串列表的非泛型类:

    public class ListOfStrings : List<string> { }
    
    推荐文章