首页 > 学院 > 开发设计 > 正文

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

2019-11-10 17:56:52
字体:
来源:转载
供稿:网友

一、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>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表