1.1 利用UIElement.OpacityMask屬性,用於改變對象區域的不透明度的畫筆。能夠使元素的特定區域透明或部分透明,從而實現比較新穎的效果。前端
1.2 OpacityMask屬性接受任何畫刷,可利用LinearGradientBrush線性漸變畫刷,經過對漸變畫刷中各顏色點加以動畫處理便可。web
漸變淡入效果,可經過事件觸發器觸發Loaded事件實現,因此能夠僅用前端XAML語言實現。後端
2.1 設置對象的OpacityMask屬性動畫
<Grid.OpacityMask> <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0"> <GradientStop Color="#00000000" Offset="0"/> <GradientStop Color="#00000000" Offset="1"/> <GradientStop Color="#00000000" Offset="1"/> </LinearGradientBrush> </Grid.OpacityMask>
2.2 設置對象的事件觸發器ui
<Grid.Triggers> <EventTrigger RoutedEvent="Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation From="1" To="0" Duration="0:0:1.5" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/> <DoubleAnimation From="1" To="0" Duration="0:0:1" BeginTime="0:0:0.5" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/> <ColorAnimation To="#FF000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Grid.Triggers>
漸變淡出效果,因爲事件觸發器事件須要路由事件觸發,因此須要使用後端代碼實現。this
3.1 準備漸變淡出的動畫和畫刷資源spa
<Window.Resources> <Storyboard x:Key="ClosedStoryboard" Storyboard.TargetName="LoginGrid"> <DoubleAnimation From="1" To="0" Duration="0:0:2" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[1].Offset"/> <DoubleAnimation From="1" To="0" Duration="0:0:1.5" BeginTime="0:0:0.5"Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Offset"/> <ColorAnimation To="#00000000" Duration="0" Storyboard.TargetProperty="OpacityMask.(GradientBrush.GradientStops)[2].Color"/> </Storyboard> <LinearGradientBrush x:Key="ClosedBrush" StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="#FF000000" Offset="0"/> <GradientStop Color="#FF000000" Offset="1"/> <GradientStop Color="#FF000000" Offset="1"/> </LinearGradientBrush> </Window.Resources>
3.2 後端經過綁定按鈕的Click事件實現code
private void btnCancel_Click(object sender, RoutedEventArgs e) { this.IsEnabled = false; LoginGrid.OpacityMask = this.Resources["ClosedBrush"] as LinearGradientBrush; Storyboard std = this.Resources["ClosedStoryboard"] as Storyboard; std.Completed += delegate { this.Close(); }; std.Begin(); }
4.1 效果圖以下對象