11

我有一个带有故事板的用户控件,我想在控件的可见性更改时停止动画。

我创建了一个触发器来暂停动画并根据状态启动它,但我不断收到 ArgumentException。

这是 XAML:

<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard x:Name="ProgressAnimation_BeginStoryboard" Storyboard="{StaticResource ProgressAnimation}"/>
    </EventTrigger>
    <Trigger Property="Control.Visibility" Value="Collapsed">
        <PauseStoryboard BeginStoryboardName="ProgressAnimation_BeginStoryboard" />
    </Trigger>
    <Trigger Property="Control.Visibility" Value="Visible">
        <ResumeStoryboard BeginStoryboardName="ProgressAnimation_BeginStoryboard" />
    </Trigger>
</UserControl.Triggers>

这是例外:

值“System.Windows.Media.Animation.PauseStoryboard”不是“System.Windows.SetterBase”类型,不能在此通用集合中使用。参数名称:值

我将如何在 XAML 中执行此操作?

谢谢,劳尔

4

2 回答 2

18

您可以使用控制模板来执行此操作:

<ControlTemplate>
    ... Control stuff here

    <ControlTemplate.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource AnimationStoryboard}" x:Name="AnimationBeginStoryboard"/>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="AnimationBeginStoryboard"/>
            </Trigger.ExitActions>
        </Trigger>
    </ControlTemplate.Triggers>

</ControlTemplate>
于 2012-02-28T11:39:13.843 回答
-2

看看MSDN 中的这个示例

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <StackPanel>
  <Rectangle Name="TargetRect" Width="200" Height="200" Fill="Blue"/>
  <Button Name="Begin">BeginStoryboard</Button>
  <Button Name="Pause">PauseStoryboard</Button>
  <Button Name="Resume">ResumeStoryboard</Button>
  <StackPanel.Triggers>
   <EventTrigger SourceName="Begin" RoutedEvent="Button.Click">
    <BeginStoryboard Name="ColorStoryboard">
     <Storyboard TargetName="TargetRect">
      <ColorAnimation Storyboard.TargetProperty="Fill.Color" To="Red" Duration="0:0:3" RepeatBehavior="Forever" AutoReverse="True"/>
     </Storyboard>
    </BeginStoryboard>
   </EventTrigger>
   <EventTrigger SourceName="Pause" RoutedEvent="Button.Click">
    <PauseStoryboard BeginStoryboardName="ColorStoryboard"/>
   </EventTrigger>
   <EventTrigger SourceName="Resume" RoutedEvent="Button.Click">
    <ResumeStoryboard BeginStoryboardName="ColorStoryboard"/>
   </EventTrigger>
  </StackPanel.Triggers>
 </StackPanel>
</Page>

我能看到的唯一区别是他们在EventTrigger这里使用,但它应该表现相同,至少在我的经验中。

于 2010-01-05T23:23:47.573 回答