Converter的使用
Converter<TInput, TOutput> Delegate
概述:Represents a method that converts an object from one type to another type.
作用於泛型集合的類型轉換,主要是用於ConverAll函數
Demo:
using System;
using System.Drawing;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<PointF> lpf = new List<PointF>();
lpf.Add(new PointF(27.8F, 32.62F));
lpf.Add(new PointF(99.3F, 147.273F));
lpf.Add(new PointF(7.5F, 1412.2F));
Console.WriteLine();
foreach( PointF p in lpf )
{
Console.WriteLine(p);
}
List<Point> lp = lpf.ConvertAll(
new Converter<PointF, Point>(PointFToPoint));
Console.WriteLine();
foreach( Point p in lp )
{
Console.WriteLine(p);
}
}
public static Point PointFToPoint(PointF pf)
{
return new Point(((int) pf.X), ((int) pf.Y));
}
}
/* This code example produces the following output:
{X=27.8, Y=32.62}
{X=99.3, Y=147.273}
{X=7.5, Y=1412.2}
{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
*/
IValueConverter Interface
概述:Exposes methods that allow modifying the data as it passes through the binding engine.
在Windows Phone開發時候,有些數據需要綁定,但是綁定時候格式或者類型需要改變。
可以用一個Converter實現IValueConvert接口,然後實現Convert方法和ConvertBack方法(主要是Convert就行)
MSDN:
Windows Phone Toolkit DateTime Converters: