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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

第15章 動(dòng)畫基礎(chǔ)(3)——XAML內(nèi)聯(lián)計(jì)算的實(shí)現(xiàn)

2019-11-10 17:56:52
字體:
供稿:網(wǎng)友

一、XAML內(nèi)聯(lián)計(jì)算

①定義一個(gè)類實(shí)現(xiàn)IValueConverter接口

②在窗口資源中導(dǎo)入定義的類

<Window.Resources>    <local:ArithmeticConverter x:Key="converter"></local:ArithmeticConverter></Window.Resources>③使用定義的類實(shí)現(xiàn)內(nèi)聯(lián)計(jì)算,如Storyboard的To屬性的設(shè)置:
To="{Binding ElementName=window,Path=Width,Converter={StaticResource converter},ConverterParameter=-30}"二、實(shí)例代碼演示①ArithmeticConverter.cs實(shí)現(xiàn)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.");        }    }}②內(nèi)聯(lián)計(jì)算的使用
<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>


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 类乌齐县| 石棉县| 荃湾区| 罗山县| 鹰潭市| 塔河县| 大宁县| 黎川县| 青神县| 焦作市| 奉节县| 永德县| 甘泉县| 即墨市| 伊宁县| 景宁| 丽江市| 江川县| 建瓯市| 苗栗市| 新宁县| 怀来县| 长沙县| 长寿区| 油尖旺区| 二手房| 惠水县| 灵寿县| 崇礼县| 榆林市| 菏泽市| 定南县| 故城县| 西华县| 岫岩| 济宁市| 洪江市| 石泉县| 图木舒克市| 永州市| 辛集市|