码迷,mamicode.com
首页 > 其他好文 > 详细

TypeConverter

时间:2014-12-08 00:43:32      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

XAML中的Attribute只接受String类型的值,例如:
  1. <Grid Background="Red"/>
问题来了,XAML解释器是如何将这个"Red"解释为Backgroud可接受类型的?事实上,这里使用了类型转换器(TypeConverter)。所谓类型转换器,顾名思义就是从一种类型转换为另一种类型的手段。简而言之就是将String类型的Red转换成Color类型了。

这里举两个简单示例来阐述如何使用TypeConvert。

示例1:Name转换为Human

先定义一个Human类,包含两个字段 Name 和 Child。
  1. class Human
  2. {
  3. private string name;
  4. private Human child;
  5. public string Name
  6. {
  7. get { return name; }
  8. set { name = value; }
  9. }
  10. public Human Child
  11. {
  12. get { return child; }
  13. set { child = value; }
  14. }
  15. public Human()
  16. {
  17. Name = string.Empty;
  18. Child = null;
  19. }
  20. public Human(string name)
  21. :this ()
  22. {
  23. Name = name;
  24. }
  25. }
然后需要定义TypeConverter这个类。首先有个基本概念得明确,就是类型转换器的Source和Target。对于这个例子,使用String类型作为Source,Human作为Target,当然反过来也没有任何问题。于是就新建一个类型转换器 NameToHumanTypeConverter~
第一点:任何的类型转换器都必须继承于TypeConverter类。
第二点:需要重写方法
  • CanConvertFrom
  • ConvertFrom
  • CanConverTo
  • ConvertTo
注意一个方向的问题,From(String -> Human),To(Human -> String)。可以看到类型转换器是支持双向操作的,这个示例只用到From就足以。
  1. public class NameToHumanTypeConverter : TypeConverter
  2. {
  3. public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
  4. {
  5. if (sourceType == typeof(string))
  6. return true;
  7. else
  8. return base.CanConvertFrom(context, sourceType);
  9. }
  10. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  11. {
  12. if (value != null && value is string)
  13. {
  14. Human human = new Human();
  15. human.Name = (string)value;
  16. return human;
  17. }
  18. else
  19. {
  20. return base.ConvertFrom(context, culture, value);
  21. }
  22. }
  23. }
接下来将这个类型转换器以Attribute的形式添加到Human类,添加的目的是为了实现自动转化,第二个示例会说明这个问题。
  1. [TypeConverter(typeof(NameToHumanTypeConverter))]
  2. class Human
  3. {
  4. // ...
  5. }
修改XAML及后台事件代码。
  1. <Window x:Class="类型转换01.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:codes="clr-namespace:类型转换01"
  5. Title="MainWindow" Height="350" Width="525">
  6. <Window.Resources>
  7. <codes:Human x:Key="man" Name="Louis" Child="Liu"></codes:Human>
  8. </Window.Resources>
  9. <Grid>
  10. <Button Click="Button_Click"></Button>
  11. </Grid>
  12. </Window>
  1. private void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3. Human human = this.FindResource("man") as Human;
  4. MessageBox.Show(string.Format("Man: {0}\nChild: {1}", human.Name, human.Child.Name));
  5. }
运行程序查看效果:
bubuko.com,布布扣
可以看到成功地将 Louis 和 Liu 转换成了两个Human对象。

示例2:Coordinate转换为String

大部分内容和示例1一致,这个示例仅仅是为了说明给类添加类型转换器的必要性,直接上代码:
若要使用如下的TypeDescriptor.GetConverter这个静态方法,则必须给Coordinate类附加类型转换器这个Attribute。
  1. static void Main(string[] args)
  2. {
  3. CoordinateToStringTypeConverter converter = new CoordinateToStringTypeConverter();
  4. Coordinate testCoordinate = new Coordinate(1.2, 3.4);
  5. string resultString = (string)converter.ConvertFrom(testCoordinate);
  6. Console.WriteLine(resultString);
  7. resultString = (string)TypeDescriptor.GetConverter(typeof(Coordinate)).ConvertFrom(testCoordinate);
  8. Console.WriteLine(resultString);
  9. string testString = "5.6,7.8";
  10. Coordinate resultCoordinate = (Coordinate)converter.ConvertTo(testString, typeof(Coordinate));
  11. Console.WriteLine(resultCoordinate);
  12. resultCoordinate = (Coordinate)TypeDescriptor.GetConverter(typeof(Coordinate)).ConvertTo(testString, typeof(Coordinate));
  13. Console.WriteLine(resultCoordinate);
  14. }

另外在MSDN上看到一段内容,说是Markup Extension的结果不能再作为TypeConverter的Source使用,原文如下:
Markup extensions and type converters fill orthogonal roles in terms of XAML processor behavior and the scenarios that they are applied to. Although context is available for markup extension usages, type conversion behavior of properties where a markup extension provides a value is generally is not checked in the markup extension implementations. In other words, even if a markup extension returns a text string as its ProvideValue output, type conversion behavior on that string as applied to a specific property or property value type is not invoked, Generally, the purpose of a markup extension is to process a string and return an object without any type converter involved.





附件列表

     

    TypeConverter

    标签:des   style   blog   http   io   ar   color   os   使用   

    原文地址:http://www.cnblogs.com/liuzeyuzh/p/4150131.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!