您的位置:首页 > 其它

第15章 动画基础(3)——XAML内联计算的实现

2017-02-08 16:49 417 查看
一、XAML内联计算

①定义一个类实现IValueConverter接口

②在窗口资源中导入定义的类

<Window.Resources>
<local:ArithmeticConverter x:Key="converter"></local:ArithmeticConverter>
</Window.Resources>
③使用定义的类实现内联计算,如Storyboard的To属性的设置:
To="{Binding ElementName=window,Path=Width,Converter={StaticResource converter},ConverterParameter=-30}"
二、实例代码演示
①ArithmeticConverter.cs实现IValueConverter接口
using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

namespace Animation
{
public class ArithmeticConverter : IValueConverter
{
private const string ArithmeticParseExpression = "([+\\-*/]{1,1})\\s{0,}(\\-?[\\d\\.]+)";
private Regex arithmeticRegex = new Regex(ArithmeticParseExpression);

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{

if (value is double && parameter != null)
{
string param = parameter.ToString();

if (param.Length > 0)
{
Match match = arithmeticRegex.Match(param);
if (match != null && match.Groups.Count == 3)
{
string operation = match.Groups[1].Value.Trim();
string numericValue = match.Groups[2].Value;

double number = 0;
if (double.TryParse(numericValue, out number)) // this should always succeed or our regex is broken
{
double valueAsDouble = (double)value;
double returnValue = 0;

switch (operation)
{
case "+":
returnValue = valueAsDouble + number;
break;

case "-":
returnValue = valueAsDouble - number;
break;

case "*":
returnValue = valueAsDouble * number;
break;

case "/":
returnValue = valueAsDouble / number;
break;
}

return returnValue;
}
}
}
}

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}

}
}
②内联计算的使用
<Window x:Class="Animation.XamlAnimation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XamlAnimation" Height="300" Width="300" Name="window"
xmlns:local="clr-namespace:Animation"
>
<Window.Resources> <local:ArithmeticConverter x:Key="converter"></local:ArithmeticConverter> </Window.Resources>
<Button Padding="10" Name="cmdGrow" Height="40" Width="160"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width"
To="{Binding ElementName=window,Path=Width,Converter={StaticResource converter},ConverterParameter=-30}"
Duration="0:0:5"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="Height"
To="{Binding ElementName=window,Path=Height,Converter={StaticResource converter},ConverterParameter=-50}"
Duration="0:0:5"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
<Button.Content>
Click and Make Me Grow
</Button.Content>
</Button>
</Window>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WPF动画