zoukankan      html  css  js  c++  java
  • 译:泛型类的标记扩展

    译文出处:http://www.codeproject.com/Tips/871592/Markup-Extension-for-Generic-classes

    标记扩展运行你再Xaml中声明泛型类。

    简介:

    我们经常要在Xaml中的style或DataTemplate或相似的地方使用泛型类。

    然而.Net不允许在xaml中声明泛型。我们需要解决这个问题。本文提供的解决方案已经在很多案例中解决这个问题。

    使用代码:

    通过使用这里提供的新的扩展,你就能够在xaml中使用泛型。

    <DataTemplate x:Key="genericTemplate" DataType="{ge:Generic GenericTypeName=System.Collections.Generic.KeyValuePair, GenericTypeAssemblyName=mscorlib, ArgumentType1={x:Type sys:String},ArgumentType2={x:Type sys:String}}">
                <StackPanel Orientation="Horizontal">
                    <Label Background="Green" Content="{Binding Path=Key}" />
                    <Label Background="Yellow" Content="{Binding Path=Value}" />
                </StackPanel>
            </DataTemplate>

    在我们的标记扩展类添加下面的属性:

    public string GenericTypeName { get; set; }
            public string GenericTypeAssemblyName { get; set; }
            public Type ArgumentType1 { get; set; }
            public Type ArgumentType2 { get; set; }
            public Type ArgumentType3 { get; set; }

    添加ProvideValue方法:

    public override object ProvideValue(IServiceProvider serviceProvider)
            {
                var genericArguments = new List<Type>();
                if(ArgumentType1 != null)
                {
                    genericArguments.Add(ArgumentType1);
                }
                if(ArgumentType2 != null)
                {
                    genericArguments.Add(ArgumentType2);
                }
                if(ArgumentType3 != null)
                {
                    genericArguments.Add(ArgumentType3);
                }
    
                if(!string.IsNullOrWhiteSpace(GenericTypeName) && !string.IsNullOrWhiteSpace(GenericTypeAssemblyName) && genericArguments.Count > 1)
                {
                    var genericType = Type.GetType(string.Format(_genericTypeFormat, GenericTypeName, genericArguments.Count, GenericTypeAssemblyName));
                    if(genericType != null)
                    {
                        var returnType = genericType.MakeGenericType(genericArguments.ToArray());
                        return returnType;
                    }
                }
                return null;
            }

    注意这儿有一个常量genericTypeFormat的声明如下:

    private const string _genericTypeFormat="{0}`{1},{2}";

    ~~

  • 相关阅读:
    堆和栈的区别
    熟悉熟悉常用的几个算法用JS的实现
    JS设置CSS样式的几种方式
    javascript的基本语法、数据结构
    DOM的概念及子节点类型
    三列自适应布局
    Javascript中括号“[]”的多义性
    Javascript中大括号“{}”的多义性
    Javascript小括号“()”的多义性
    JavaScript 开发者经常忽略或误用的七个基础知识点
  • 原文地址:https://www.cnblogs.com/yplong/p/4266148.html
Copyright © 2011-2022 走看看