【转】WPF 窗体淡入淡出动画

时间:2021-03-11 21:31:51

第一种

<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="00:00:01" Storyboard.TargetProperty="Opacity" From="0" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>

第二种

<Window.Resources>
<Storyboard x:Key="showMe">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" To="0.75"/>
</Storyboard>
<Storyboard x:Key="hideMe">
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" To="0.0"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
public new void Show()
{
(FindResource("showMe") as System.Windows.Media.Animation.Storyboard).Begin(this);
}
public new void Hide()
{
(FindResource("hideMe") as System.Windows.Media.Animation.Storyboard).Begin(this);
}