重用现有的Storyboard

4
这是一种可能的做法:
<Button.Triggers>         
  <EventTrigger RoutedEvent="Button.Click">    
    <BeginStoryboard>
      <Storyboard>
        <ColorAnimation 
          Storyboard.TargetName="myAnimatedBrush"
          Storyboard.TargetProperty="Color"
          From="Red" To="Blue" Duration="0:0:7" />
      </Storyboard>
    </BeginStoryboard>
  </EventTrigger>
</Button.Triggers>

“但是假设我有:”
      <Storyboard x:Name="name">
        <ColorAnimation 
          Storyboard.TargetName="myAnimatedBrush"
          Storyboard.TargetProperty="Color"
          From="Red" To="Blue" Duration="0:0:7" />
      </Storyboard>

并希望可以多次重复使用它。

<Button.Triggers>         
  <EventTrigger RoutedEvent="Button.Click">    
    <BeginStoryboard>
      //
      // <--->  what whould I put here??
      //
    </BeginStoryboard>
  </EventTrigger>
</Button.Triggers>

我只对XAML感兴趣,不对c#感兴趣。
编辑:
在我使用回答中的建议后,出现了一个错误:

属性{StaticResource myStoryboard}的值超出范围。


相关 - https://dev59.com/elLTa4cB1Zd3GeqPaXDC - ChrisF
1
这真的是Silverlight吗?Silverlight仅支持Loaded事件的EventTrigger。 - AnthonyWJones
2个回答

2

将其作为资源并使用StaticResource调用它。

[考虑所有资源都在App.xaml中定义]

<Application.Resources>
    <Storyboard x:Key="MyStoryboard">
        ....
    </Storyboard>
</Application.Resources>

然后,通过按钮的操作

<Button.Triggers>         
    <EventTrigger RoutedEvent="Button.Click">    
        <BeginStoryboard Storyboard="{StaticResource MyStoryboard}"
                         x:Name="MyStoryboard_Begin"/>
    </EventTrigger>
<Button.Triggers>

注意:使用x:Name并非必要,但如果您想确保在运行另一个故事板之前停止此故事板,则很有用:在另一个触发器中使用StopStoryboard


0

您可以使用存储在ResourceDictionary中的StaticResource

ResourceDictionary可以在专用文件中定义,也可以在相关容器内部定义;例如,您可以使用全局专用资源文件来加载应用程序中的资源(WPF中加载主题的示例),或者本地提供“私有”资源,这些资源仅公开给相关控件使用(尽管可能仍可通过完全限定路径或某种巫术访问)。

因此,要定义它...

<UserControl.Resources>
  <Storyboard x:Key="myStoryboard">
    <ColorAnimation 
      Storyboard.TargetName="myAnimatedBrush"
      Storyboard.TargetProperty="Color"
      From="Red" To="Blue" Duration="0:0:7" />
  </Storyboard>
</UserControl.Resources>

而要利用它...

<... Storyboard="{StaticResource myStoryboard}" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接