×
Namespaces

Variants
Actions

Windows Phone 应用程序的值转换器集合

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon File.png
WP Metro Icon UI.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

代码示例
兼容于
文章
翻译:
Hazel Xie
最后由 hamishwillee 在 28 Jun 2013 编辑

本文给出了一些用于Windows Phone应用程序不同情况下的值转换器的例子

Contents

介绍

值转换器(IValueConverter Interface)是一种多功能工具,对在Windows Phone(Windows 8现代用户界面或WPF)平台的数据绑定情况有帮助。其包括

  • 数据类型转换,即 BooleanToVisibilityConverter
  • 绑定数据操作,即BooleanNegationConverter
  • 个别文字转换,即TimeToStringConverter
  • 特效,如动态画图,即 TimeToShapeConverter
  • 调试目的, 即. DatabindingDebugConverter 参考如何调试windows phone数据绑定问题

使用转换器的必要步骤介绍也在如何调试windows phone数据绑定问题中 另外一个非常有趣的想法是一个值转换器的转换功能可以通过脚本生成:The Last IValueConverter. 这是示例应用程序的截图:

ConverterScreen.png


注:IValueConverter interface定义了 Win8现代UI又名“Metro”又名Windows 8的商店应用程序的不同功能参数。这就是为什么我不能使用Portable Class Library(便携式类库)来涵盖转换器类。这也意味着改变Convert和ConvertBack函数中最后两个功能参数,这些转换器可用于Windows 8。

BooleanToVisibilityConverter

这个转换器可以转换布尔值成大多数xaml元素需要的可见属性值类型。它主要用于两种情况中: 当一个Checkbox或ToggleButton元素交互控制另一个元素的可见性或者当后台数据执行同样的操作。 对于第一种情况,可见性属性的数据绑定语法参考控制的XAML元素的IsChecked属性,包括转换器:

<toolkit:ToggleSwitch Name="tgVis" Content="Clock Visible" IsChecked="True" 
Checked="ToggleSwitch_Checked" Unchecked="ToggleSwitch_Checked"/>
<Path Data="{Binding CurrentTime, Converter={StaticResource TimeToShapeConverter}}"
Visibility="{Binding IsChecked, ElementName=tgVis,Converter={StaticResource BooleanToVisibilityConverter}}"
Stroke="Gray" StrokeThickness="12" StrokeEndLineCap="Round" StrokeStartLineCap="Round"
Stretch="Uniform" Margin="6"/>

第二种情况使用比较标准的数据

<Textblock Text="Make me invisible"
Visibility="{Binding VisibilityProperty,Converter={StaticResource BooleanToVisibilityConverter}}"/>

这是转换器的简单代码:

/// <summary>
/// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
/// <see cref="Visibility.Collapsed"/>.
/// </summary>
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value is Visibility && (Visibility)value == Visibility.Visible;
}
}

BooleanNegationConverter

这个可用于反转布尔值,即,如果一个布尔值和它的相反值都需要时。

/// <summary>
/// Value converter that translates true to false and vice versa.
/// </summary>
public sealed class BooleanNegationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return !(value is bool && (bool)value);
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return !(value is bool && (bool)value);
}
}

在这代码片段中,转换器用于显示切换的结果:

<StackPanel Orientation="Horizontal">
<TextBlock Text="Toggle to switch to "/>
<TextBlock Text="{Binding IsChecked, ElementName=tgVis,Converter={StaticResource BooleanNegationConverter}}"/>
</StackPanel>
</code
== TimeToStringConverter ==
Display a date or time from a DateTime instance to displayable string using the TimeToStringConverter.
<code csharp>
/// <summary>
/// Value converter that converts a DateTime to a text and vice versa.
/// </summary>
public sealed class TimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTime dt = (DateTime)value;
return dt.ToString();
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
DateTime dt;
DateTime.TryParse(value.ToString(), out dt);
return dt;
}
}

TimeToShapeConverter

使用几何图形对象转换时间到时钟图片。 这是非常基本的实现操作,但是我认为你了解什么你能做…

/// <summary>
/// Value converter that creates a clock image from a time value.
/// Reference the System.Windows.Media namespace.
/// You can bind the result to a Path element.
/// </summary>
public sealed class TimeToShapeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTime dt = (DateTime)value;
GeometryGroup coll = new GeometryGroup();
EllipseGeometry ell = new EllipseGeometry();
ell.Center = new Point(55, 55);
ell.RadiusX = ell.RadiusY = 60;
coll.Children.Add(ell);
LineGeometry hour = new LineGeometry();
double deg = (dt.Hour % 12) * Math.PI / 6;
hour.StartPoint = ell.Center;
hour.EndPoint = new Point(55 + Math.Sin(deg) * 35, 55 - Math.Cos(deg) * 35);
coll.Children.Add(hour);
LineGeometry minute = new LineGeometry();
minute.StartPoint = ell.Center;
deg = dt.Minute * Math.PI / 30;
minute.EndPoint = new Point(55 + Math.Sin(deg) * 50, 55 - Math.Cos(deg) * 50);
coll.Children.Add(minute);
return coll;
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}

关于怎么在xaml中使用,请参考上面BooleanToVisibilityConverter的例子.

DatabindingDebugConverter

这个转换器在如何调试windows phone数据绑定问题文章中有相关描述。

/// <summary>
/// Value Converter that does nothing except giving the chance to set a breakpoint.
/// </summary>
public sealed class DatabindingDebugConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value;
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value;
}
}

邀请

请随意添加更多值转换器示例!

示例代码

本文附仅使用两个时间转换器以字符和模拟时钟显示当前Media: ValueConverterSample.zipBooleanToVisibilityConverter用于控制开关切换时钟的可见性。

This page was last modified on 28 June 2013, at 09:40.
129 page views in the last 30 days.