WPF:网格可见性淡出不像淡入

时间:2018-07-10 13:38:52

标签: wpf animation grid storyboard

所以我有这个Grid

<Grid x:Name="notificarionGrid" Visibility="Hidden"/>

我想用动画更改其Visibility的值,所以我有这个2 Storyboard

<Storyboard x:Key="fadeIn">
            <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimation BeginTime="0:0:0.0" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:2.0"/>
        </Storyboard>

        <Storyboard x:Key="fadeOut">
            <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Hidden</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimation BeginTime="0:0:5.0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2.0"/>
        </Storyboard>

后面的代码:

淡入

Storyboard storyboard = Resources["fadeIn"] as Storyboard;
            storyboard.Begin(notificarionGrid);

淡出

Storyboard storyboard = Resources["fadeOut"] as Storyboard;
            storyboard.Begin(notificarionGrid);

唯一的问题是Grid在淡出时立即消失,不像淡入那样需要2秒钟。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

ObjectAnimationUsingKeyFrames中删除将Visibility属性设置为Hidden的{​​{1}}:

Storyboard

如果在为元素设置<Storyboard x:Key="fadeOut"> <DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2.0"/> </Storyboard> 动画之前隐藏该元素,则将无法看到淡入淡出的动画。