(一)WPF中的动画

动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互。这里我们讨论一下故事板。

在WPF中我们采用Storyboard(故事板)的方式来编写动画,为了对Storyboard有个大概的印象,你可以粘贴以下代码到XamlPad来查看效果:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="Storyboards Example">
<StackPanel Margin="20"> <Rectangle Name="MyRectangle"
Width="100"
Height="100">
<Rectangle.Fill>
<SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" />
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Page.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimation
Storyboard.TargetName="MyRectangle"
Storyboard.TargetProperty="Width"
From="100" To="200" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</StackPanel>
</Page>

在介绍Storyboard之前应该先了解Animation
Animation提供一种简单的“渐变”动画,我们为一个Animation指定开始值和一个结束值,并指定由开始值到达结束值所需的时间,便可形成一个简单的动画。比如我们指定长方形的宽度由100变化到200,所需时间为1秒,很容易想像这样的动画是什么样的,而它对应的Xaml代码如下:

            <DoubleAnimation
Storyboard.TargetName="MyRectangle"
Storyboard.TargetProperty="Width"
From="100" To="200" Duration="0:0:1" />

将它翻译成C#代码则如下:

            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = ;
myDoubleAnimation.To = ;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds());
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));

代码里我们定义了一个DoubleAnimation,并指定了它的开始值和结束值以及它由开始值到达结束值所需的时间。至于后面两句,它们是用来将Aniamtion与指定的对象和指定的属性相关联,等会我们将介绍。
注意到,这里我们使用的是DoubleAnimation,因为我们所要变化的是数值。那么如果我们要变化颜色是不是就用ColorAnimation了呢,对,其实出了这些之外还有PointAnimation等等,并且你可以实现IAnimatable接口来实现自定义版本的Animation。关于这些你可以参见System.Windows.MediaAniamtion名字空间.

但值得注意的是并非每个属性都能够使用Animation,它必须满足以下条件:
1,它必须是Dependency Property
2,它所在类必须继承于DependencyObject,必须实现了IAnimatable接口.
3,必须有类型一致的Animation Type(即Color类型使用ColorAniamtion,Point类型使用PointAnimation等)

一个简单的Animation定义了一个简单的动画,很容易想到的是,如果若干个Animation同时作用于一个对象,那么这个对象不就可以表现复杂的动画了吗,对,这就是Storyboard

Storyboard可以看做是Animation的容器,它包含了若干的简单动画来完成一个复杂动画。
参考以下代码:

<!-- This example shows how to animate with a storyboard.-->
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="Storyboards Example">
<StackPanel Margin="20"> <Rectangle Name="MyRectangle"
Width="100"
Height="100">
<Rectangle.Fill>
<SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" />
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Page.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimation
Storyboard.TargetName="MyRectangle"
Storyboard.TargetProperty="Width"
From="100" To="200" Duration="0:0:1" />
<ColorAnimation
Storyboard.TargetName="MySolidColorBrush"
Storyboard.TargetProperty="Color"
From="Blue" To="Red" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</StackPanel>
</Page>

这里我们的Storyboard定义了DoubleAnimation来变化矩形的宽度,并定义了ColorAnimation来变化矩形的颜色。

至此,你已经可以编写绚丽的WPF动画了,并推荐你下载Expression Blend来制作WPF动画.

但你会发现使用XAML标记的方式来编写动画虽然很简单,但缺乏了C#等程序设计语言的灵活性,比如我们的矩形动画中矩形的宽度是由后台逻辑计算出来的变量值,我们的动画将如何编写呢,这时我更喜欢使用C#的方式来编写动画,虽然这所需的代码量更大.
以下重点介绍如何用C#编写动画,并且这更助于你理解Storyboard是如何工作的。

参考以下代码:

            this.Name = "PageMain";
myRectangle.Name = "MyRectangle"; NameScope.SetNameScope(this, new NameScope());
this.RegisterName(myRectangle.Name, myRectangle); DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = ;
myDoubleAnimation.To = ;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds());
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty)); Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation); this.Loaded += delegate(object sender, MouseEventArgs e)
{
myStoryboard.Begin(this);
};

其中:

DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 100;
            myDoubleAnimation.To = 200;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
定义了一个DoubleAniamtion,并指定了它的开始值和结束值以及所需的时间.
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);设置myDoubleAniamtion的作用对象是"myRectangle",注意到传入的第二个参数是一个字符串myRectangle.Name,那么我们的程序怎么知道"myRectangle"这个字符串就是指我们的矩形对象myRectangle呢,这里存在一个名称与对象的映射,即我们的"myRectangle"映射到矩形对象myRectangle,为了构造这个映射我们涉及到了NameScope(名字域)这个概念.
            NameScope.SetNameScope(this, new NameScope());                      
            this.RegisterName(myRectangle.Name, myRectangle);
上面的代码中,this设置了一个名字域,myRectagle向这个名字域注册了自己的名字,这样我们的程序就可以通过this的名字域来查找到myRectangle与"myRectangle"之间的映射关系了,关于NameScope可以参见MSDN WPF Namescopes主题.
为了让myDoubleAnimation知道它所作用的属性是谁,我们使用Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));语句来将Aniamtion与属性关联起来,其中PropertyPath中指定要作用的对象所对应的DependencyProperty.
然后我们将定义好的myDoubleAniamtion添加到myStoryboard的Children中去.最后就可以通过调用Storyboard的Begin(FrameworkElement)方法来开始我们的动画.

Begin方法的另一个重载形式是public void Begin (FrameworkContentElement containingObject,bool isControllable),第二个参数表明我们的storyboard是否是可控的,如果可控的话,我们可以像控制播放器一样控制来控制storyboard,关于控制Storyboard请参考Storyboard类中的Pause,Seek等方法.

至此也许我们会认为这些知识足以应付简单的动画了,现在让我们一起设计一个简单的动画,也许会发现些问题.

假设我们的界面中存在一个Button对象button1,我们设计一个简单的动画让它在窗口中的x坐标从0连续变化到100,然后在从100变化到0,如此重复.也许我们会编写如下的代码:

            this.button1.Name = "button1";
this.Name = "window1";
NameScope.SetNameScope(this, new NameScope());
this.RegisterName(this.button1.Name, this.button1); DoubleAnimation xAnimation = new DoubleAnimation();
xAnimation.From = ;
xAnimation.To = ;
xAnimation.Duration = new Duration(TimeSpan.FromSeconds()); Storyboard story = new Storyboard();
story.AutoReverse = true;
story.RepeatBehavior = RepeatBehavior.Forever;
story.Children.Add(xAnimation); Storyboard.SetTargetName(xAnimation, this.button1.Name);
Storyboard.SetTargetProperty(xAnimation, ???);

但当我们编写到Storyboard.SetTargetProperty(xAnimation, ???);时发现似乎不知道将我们的xAniamtion关联到哪个属性.似乎Button中没有用来控制X坐标的DependencyProperty.但通过研究后发现(你可以通过ExpressionBlend自动生成的XAML代码来发现这些信息),如果我们将button1的RenderTransform设置为TranslateTransform,然后可以通过TranslateTransform的XProperty属性来更改button1的X坐标.注意到,我们并不是像以前一样直接关联到Button的某个属性(比如先前的WidthProperty),而是通过其RenderTransformProperty属性的XProperty来间接关联的,这中方式叫做"属性链"(PropertyChain).
参考下面的代码:

            DependencyProperty[] propertyChain = new DependencyProperty[]
{
Button.RenderTransformProperty,
TranslateTransform.XProperty
}; Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(0).(1)", propertyChain));

为了构造PropertyChain,我们先定义一个DependencyProperty的数组,注意数组的元素是怎么来的,它按照属性链的"链条"关系依次书写,直到到达我们最终要修改的属性,(由于我们是通过将RenderTransformProperty设置为TranslateTransform类型,所以第二个元素是TranslateTransform.XProperty),简单地说就是(类型1.属性1,类型2.属性2,....类型n.属性n),其中类型i是属性i-1的类型或可以与之转换的类型.
这样我们的代码就演化如下:

            this.button1.RenderTransform = new TranslateTransform();

            this.button1.Name = "button1";
this.Name = "window1";
NameScope.SetNameScope(this, new NameScope());
this.RegisterName(this.button1.Name, this.button1); DoubleAnimation xAnimation = new DoubleAnimation();
xAnimation.From = ;
xAnimation.To = ;
xAnimation.Duration = new Duration(TimeSpan.FromSeconds()); DependencyProperty[] propertyChain = new DependencyProperty[]
{
Button.RenderTransformProperty,
TranslateTransform.XProperty
}; Storyboard story = new Storyboard();
story.AutoReverse = true;
story.RepeatBehavior = RepeatBehavior.Forever;
story.Children.Add(xAnimation); Storyboard.SetTargetName(xAnimation, this.button1.Name);
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(0).(1)", propertyChain)); story.Begin(this);

注意:如果你收到关于PropertyChain的运行时错误或动画没有效果,那么你应该初始化button的RenderTransform属性,所以我们添加了this.button1.RenderTransform = new TranslateTransform();语句.

=======================================================================================================================

)wpf动画——new PropertyPath属性链

在wpf中我们常用storyboard故事板装载很多的动画处理Animation,我们需要用Storyboard.SetTarget设置操作的对象,需要用Storyboard.SetTargetProperty设置操作对象的操作属性PropertyPath,本例将说明一种操作属性PropertyPath的便利方法:

1.新建一个wpf应用程序,简单修改一下xaml展示如下:

<Window x:Class="WpfApplication48.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>

2.后台cs修改如下(效果:点击button,button的横坐标x由12到300,播放过程动画):

    /// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)
{
this.button1.RenderTransform = new TranslateTransform(); Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.From = ;
da.To = ;
da.Duration = TimeSpan.FromSeconds();
sb.Children.Add(da); DependencyProperty[] propertyChain = new DependencyProperty[]
{
Button.RenderTransformProperty,
TranslateTransform.XProperty
}; Storyboard.SetTarget(da, this.button1);
Storyboard.SetTargetProperty(da, new PropertyPath("(0).(1)", propertyChain)); sb.Completed += new EventHandler((object sender1, EventArgs e1) => { MessageBox.Show("completed"); });
sb.Begin();
}
}

一般我们在写到Storyboard.SetTargetProperty时遇到new PropertyPath,

如果是简单的属性,例如Button.WidthProperty,我们可以直接new PropertyPath(Button.WidthProperty)达到目的,
但如果你发现你需要操作到的属性无法在Button中直接'.'出来,就需要用到上例用到的属性链方法:

首先定义一个属性链:

            DependencyProperty[] propertyChain = new DependencyProperty[]
{
Button.RenderTransformProperty,
TranslateTransform.XProperty
};

属性链的写法,定义一个DependencyProperty属性的数组,该数组中的元素均是Property属性,且按照从属关系先后排列,例如上例中,我们需要先将button的RenderTransform设置为TranslateTransform,然后通过TranslateTransform的XProperty来更改button的x坐标,

当然,别忘了初始化button的RenderTransform属性= new TranslateTransform(),否则动画将没有效果。

以上未经博主同意转载,敬请原谅。

【WPF学习笔记】[转]周银辉之WPF中的动画 && 晓风影天之wpf动画——new PropertyPath属性链的更多相关文章

  1. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  2. WPF 学习笔记-在WPF下创建托盘图标

    原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...

  3. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  4. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  5. VSTO学习笔记(八)向 Word 2010 中写入表结构

    原文:VSTO学习笔记(八)向 Word 2010 中写入表结构 前几天公司在做CMMI 3级认证,需要提交一系列的Word文档,其中有一种文档要求添加公司几个系统的数据库中的表结构.我临时接到了这项 ...

  6. VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表

    原文:VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表 Excel具有强大的图表显示.分析功能,这点毋庸置疑,但是如果将常规MIS系统中的数据以报表的形式在Excel中显示,却并不那 ...

  7. VSTO学习笔记(四)从SharePoint 2010中下载文件

    原文:VSTO学习笔记(四)从SharePoint 2010中下载文件 上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程.本次我们来给CO ...

  8. 【WPF学习笔记】之如何把数据库里的值读取出来然后显示在页面上:动画系列之(六)(评论处有学习资料及源码)

    (应博友们的需要,在文章评论处有源码链接地址,以及WPF学习资料.工具等,希望对大家有所帮助) ...... 承接系列五 上一节讲了,已经把数据保存到数据库并且删除数据,本讲是把已经存在的数据从数据库 ...

  9. WPF学习笔记——依赖属性(Dependency Property)

    1.什么是依赖属性 依赖属性是一种可以自己没有值,并且通过Binding从数据源获得值(依赖在别人身上)的属性,拥有依赖属性的对象被称为"依赖对象". 依赖项属性通过调用 Regi ...

随机推荐

  1. python 生成式和生成器

    #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/10/17 21:46 # @Author : lijunjiang # @Fi ...

  2. FormatDateTime 当前时间减去几小时的做法

    top_start_modified := FormatDateTime('yyyy-mm-dd hh:mm:ss',(Now - ((1/24)*3)));    top_end_modified ...

  3. 转载——Visiual Studio2012 CLR20r3问题

    看到有更新,习惯性的点了,升级到Visiual Studio Ultimate 2012 Update 1,并且按照提升重启了电脑.因为昨天太晚,也没验证.尽早打开VS,结果直接Crash.错误如下: ...

  4. AC日记——黑魔法师之门 codevs 1995

    1995 黑魔法师之门  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 经过了16个工作日的紧张 ...

  5. group by与distinct效率分析及优化措施

    如何使用group by进行去重 因为mysql的distinct在结果集中,全部不同,才可以去重.所以,当我们进行去重处理的时候,需要单独对某列进行去重,可以使用group by子句进行分组去重se ...

  6. Define Custom Data Filter Using Pre-Query Trigger In Oracle Forms

    Oracle Forms is having its default records filter, which we can use through Enter Query mode to spec ...

  7. Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason:

    Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason: Connection t ...

  8. mysql忘记密码的解决办法

    mysql忘记密码时,需要重设密码. 在Windows下的操作如下: 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant- ...

  9. MFC中 日期字符串的转换

    一.将字符串2011-08-1800:00:00转换为字符串2011-8-18,通过以下的函数 CString DataDeleteZero(CString DATA) { CStringstrmon ...

  10. EasyMvc入门教程-基本控件说明(11)菜单导航

    基本上,是个网站就会有菜单导航,目前流行的菜单是不是这样样子的? 似乎是bootstrap引领的风格,我们就用EasyMvc实现吧,上代码: @{ var data = new List<Men ...