WPF實(shí)現(xiàn)動(dòng)畫效果(七)之演示圖板
WPF動(dòng)畫效果系列
WPF實(shí)現(xiàn)動(dòng)畫效果(一)之基本概念
WPF實(shí)現(xiàn)動(dòng)畫效果(二)之From/To/By 動(dòng)畫
WPF實(shí)現(xiàn)動(dòng)畫效果(三)之時(shí)間線(TimeLine)
WPF實(shí)現(xiàn)動(dòng)畫效果(四)之緩動(dòng)函數(shù)
WPF實(shí)現(xiàn)動(dòng)畫效果(五)之關(guān)鍵幀動(dòng)畫
WPF實(shí)現(xiàn)動(dòng)畫效果(六)之路徑動(dòng)畫
WPF實(shí)現(xiàn)動(dòng)畫效果(七)之演示圖板
正文
前面所介紹的都是單一的動(dòng)畫,它只能修改單一屬性。有的時(shí)候,我們需要將一組動(dòng)畫一起進(jìn)行,對于一個(gè)按鈕,我們可能有如下需求:
選擇該按鈕時(shí),該按鈕增大并更改顏色。
單擊該按鈕時(shí),該按鈕縮小并恢復(fù)其原始大小。
該按鈕變成禁用時(shí),縮小且不透明度縮減到 50%。
每個(gè)操作都同時(shí)對應(yīng)進(jìn)行著兩個(gè)動(dòng)畫,此時(shí)用我們就需要用到TimelineGroup了,前文介紹TimeLine的時(shí)候已經(jīng)介紹過它了,它可以將多個(gè)TimeLine封裝成一個(gè)統(tǒng)一調(diào)度。但TimeLine是一個(gè)抽象基類,我們通常使用的是它的子類演示圖板(Storyboard)。
演示圖板(Storyboard) 是一種為其所包含的時(shí)間線提供目標(biāo)信息的容器時(shí)間線。 演示圖板可以包含任意類型的 Timeline,包括其他容器時(shí)間線和動(dòng)畫。
var widthAnimation = new DoubleAnimation() { To = 250, FillBehavior = FillBehavior.Stop }; var opacityAnimation = new DoubleAnimation() { From = 1, To = 0, FillBehavior = FillBehavior.Stop }; var storyBoard = new Storyboard() { Duration = TimeSpan.FromSeconds(2) }; storyBoard.Children.Add(widthAnimation); storyBoard.Children.Add(opacityAnimation); Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width")); Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); storyBoard.Begin(button);
這個(gè)例子簡單的演示了如何使用StoryBoard,由于Storyboard經(jīng)常使用與XAML,這里也介紹一下XAML中的寫法:
<Storyboard x:Key="storyBoard"> <DoubleAnimation Storyboard.TargetProperty="Width" To="250" FillBehavior="Stop"/> <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" FillBehavior="Stop"/> </Storyboard>
使用方式如下:
var storyBoard = this.FindResource("storyBoard") as Storyboard; storyBoard.Begin();
控制Storyboard
前面已經(jīng)介紹過,Storyboard 像Clock方法一樣,直接封裝了Begin、 Seek、 Stop、 Pause、Resume、Remove等幾個(gè)函數(shù),在代碼中可以直接使用。另外,在XAML中,Storyboard是可以直接在觸發(fā)器中(EventTrigger、DataTrigger、Trigger)使用的,如下就是一個(gè)簡單的例子:
<Window.Resources> <Storyboard x:Key="storyBoard"> <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="Width" To="250" FillBehavior="Stop"/> <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="Opacity" From="1" To="0" FillBehavior="Stop"/> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="Loaded" > <BeginStoryboard Storyboard="{StaticResource storyBoard}" /> </EventTrigger> </Window.Triggers>
可以看到,這兒用到了一個(gè)系統(tǒng)提供的名為BeginStoryboard的TriggerAction,同樣也提供了SeekStoryboard、 StopStoryboard、 PauseStoryboard、ResumeStoryboard、RemoveStoryboard等幾個(gè)TriggerAction。一個(gè)稍微復(fù)雜點(diǎn)的例子如下:
<Window.Resources> <Storyboard x:Key="storyBoard"> <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="Width" To="250" FillBehavior="Stop"/> <DoubleAnimation Storyboard.TargetName="button" Storyboard.TargetProperty="Opacity" From="1" To="0" FillBehavior="Stop"/> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="MouseEnter" > <BeginStoryboard Name="storyBegin" Storyboard="{StaticResource storyBoard}" /> </EventTrigger> <EventTrigger RoutedEvent="MouseLeave" > <RemoveStoryboard BeginStoryboardName="storyBegin" /> </EventTrigger> </Window.Triggers>
另外,微軟提供的Interaction也能在XAML中執(zhí)行Storyboard的控制:
<i:Interaction.Triggers> <i:EventTrigger EventName="MouseEnter"> <ei:ControlStoryboardAction Storyboard="{StaticResource storyBoard}" ControlStoryboardOption="Play" /> </i:EventTrigger> <i:EventTrigger EventName="MouseLeave"> <ei:ControlStoryboardAction Storyboard="{StaticResource storyBoard}" ControlStoryboardOption="Stop" /> </i:EventTrigger> </i:Interaction.Triggers>
由于微軟的Interaction擴(kuò)展在MVVM模式下非常有用,擴(kuò)展性也非常好,這種方式很多時(shí)候更方便。關(guān)于Interaction的使用方式,請參看這篇文章:Interaction triggers in WPF
參考資料:
到此這篇關(guān)于WPF實(shí)現(xiàn)動(dòng)畫效果之演示圖板的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#運(yùn)行程序時(shí)阻止關(guān)閉顯示器和系統(tǒng)待機(jī)
這篇文章介紹了C#運(yùn)行程序時(shí)阻止關(guān)閉顯示器和系統(tǒng)待機(jī)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06淺談C#2.0泛型中的變化:default關(guān)鍵字
下面就詳細(xì)的說明一下。之所以會(huì)用到default關(guān)鍵字,是因?yàn)樾枰诓恢李愋蛥?shù)為值類型還是引用類型的情況下,為對象實(shí)例賦初值2013-09-09c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
這篇文章主要介紹了c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟,大家參考使用吧2013-12-12DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息,需要的朋友可以參考下2014-08-08C#調(diào)用存儲(chǔ)過程詳解(帶返回值、參數(shù)輸入輸出等)
這篇文章主要介紹了C#調(diào)用存儲(chǔ)過程的方法,結(jié)合實(shí)例形式詳細(xì)分析了各種常用的存儲(chǔ)過程調(diào)用方法,包括帶返回值、參數(shù)輸入輸出等,需要的朋友可以參考下2016-06-06實(shí)例解析C#設(shè)計(jì)模式編程中簡單工廠模式的使用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中簡單工廠模式的使用,文中也舉了在.NET框架下簡單工廠模式的實(shí)現(xiàn)例子,需要的朋友可以參考下2016-02-02