wpf淡入动画只工作一次

时间:2016-03-11 09:36:21

标签: c# wpf animation

我创建了一个淡入淡出动画,由视图模型中的属性更改触发。这在第一次淡入和淡出时工作正常,但每次重复此操作时,仅显示淡出,这意味着控件保持不可见状态,直到它闪烁到不透明度1然后淡出。

XAML:

<DataTemplate x:Key="MessageTemplate">
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding FadeInAnimationState}" Value="Active">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
        <DataTrigger Binding="{Binding FadeOutAnimationState}" Value="Active">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </DataTemplate.Triggers>
    <StatusBarItem>
        ...
    </StatusBarItem>
</DataTemplate>
...
<ContentPresenter ContentTemplate="{StaticResource MessageTemplate}" Content="{Binding}"/>

负责触发动画的Viewmodel属性:

private DisplayState _messageState;
private DisplayState MessageState
{
    get { return _messageState; }
    set
    {
        _messageState = value;
        if (value == DisplayState.Displayed)
        {
            FadeOutAnimationState = AnimationState.Inactive;
            FadeInAnimationState = AnimationState.Active;
        }
        else
        {
            FadeInAnimationState = AnimationState.Inactive;
            FadeOutAnimationState = AnimationState.Active;
        }
    }
}

public AnimationState FadeInAnimationState
{
    ... // getter and setter with NotfiyPropertyChanged
}

public AnimationState FadeOutAnimationState
{
    ... // getter and setter with NotfiyPropertyChanged
}

呼叫(调试):

MessageState = DisplayState.Displayed;
await Task.Delay(duration);
MessageState = DisplayState.Hidden;
await Task.Delay(TimeSpan.FromSeconds(1));

我在做什么?

1 个答案:

答案 0 :(得分:2)

完成后,您需要删除故事板。类似的东西:

 <DataTrigger.EnterActions>
   <RemoveStoryboard BeginStoryboardName="FadeIn" />
   <RemoveStoryboard BeginStoryboardName="FadeOut" />       
   <BeginStoryboard Name="FadeIn">           
       <Storyboard>
          ...
       </Storyboard>
   </BeginStoryboard>
 </DataTrigger.EnterActions>

(对FadeOut也一样,给它起一个名字)

或者,您可以在ExitActions

上执行此操作
 <DataTrigger.EnterActions>
   <BeginStoryboard Name="FadeIn">           
       <Storyboard>
          ...
       </Storyboard>
   </BeginStoryboard>
 </DataTrigger.EnterActions>
 <DataTrigger.ExitActions>
   <RemoveStoryboard BeginStoryboardName="FadeIn" />
 </DataTrigger.ExitActions>

否则最后一个故事板将保持&#34;推动&#34;它的最后一个值,所以你不会看到任何变化