国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

第15章 動畫基礎(3)——XAML內聯計算的實現

2019-11-10 18:40:50
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 依兰县| 恩平市| 唐山市| 墨竹工卡县| 南宁市| 石景山区| 武平县| 嘉善县| 朝阳县| 左云县| 衡水市| 巧家县| 南安市| 怀仁县| 吴川市| 临武县| 乐昌市| 奉新县| 宁津县| 齐齐哈尔市| 临颍县| 兰溪市| 秦皇岛市| 宣武区| 金沙县| 石渠县| 忻州市| 新平| 吴忠市| 如皋市| 大关县| 靖州| 微山县| 衡东县| 城口县| 邛崃市| 巩义市| 河津市| 桓台县| 石阡县| 凤台县|