第15章 动画基础(3)——XAML内联计算的实现
2017-02-08 16:49
417 查看
一、XAML内联计算
①定义一个类实现IValueConverter接口
②在窗口资源中导入定义的类
①ArithmeticConverter.cs实现IValueConverter接口
①定义一个类实现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>
相关文章推荐
- 利用Java Applet编程实现动画特技-Java基础-Java-编程开发
- iPhone开发基础之动画显示图片的实现
- unity基础开发----unity中的UV动画实现代码
- Android基础笔记——动画(补间动画:Tween xml实现)
- UI基础--手写代码实现汤姆猫动画
- 实现文本自动分类的基础----Term频率计算方法
- 3d数学基础-计算行列式-用C++代码实现
- 在Android动画基础上实现自定义的动画效果
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【Android 基础】Animation 动画介绍和实现
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- WPF 各种基础动画实现
- 游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 实现文本自动分类的基础--Term频率计算方法
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- android基础---简易闪屏操作,通过动画实现
- 【Android 基础】Animation 动画介绍和实现