WP7 - 使用Application.Resources中定义的Storyboard

Pra*_*ian 6 silverlight wpf windows-phone-7

在我的Application.Resources我有以下Storyboard定义.

<Application.Resources>
  <!--Storyboard animation for fading out a UI element-->
  <Storyboard x:Key="FadeOutAnimation">
    <DoubleAnimation From="1"
                     To="0"
                     Duration="0:0:0.25"
                     Storyboard.TargetProperty="Opacity"
                     AutoReverse="False" />
  </Storyboard>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

在代码隐藏中,TextBlock当用户点击它时,我会使用它来淡出一些s.

// Get the storyboard from application resources
Storyboard sb = (Storyboard)App.Current.Resources["FadeOutAnimation"];
// Setup the animation target for fade out
Storyboard.SetTarget( sb.Children.ElementAt( 0 ) as DoubleAnimation, myTextBlock );
// Set the animation completed handler
sb.Completed += ( s, e1 ) => {
  // Stop the Storyboard
  sb.Stop();
  // Hide the TextBlock
  myTextBlock.Visibility = Visibility.Collapsed;
};
// Start the Storyboard
sb.begin();
Run Code Online (Sandbox Code Playgroud)

问题是,我是否需要以某种方式'解除' myTextBlock成为目标DoubleAnimation

如果是,我该怎么办?

我问的原因是我担心在TextBlock再次使用这个故事板之前会有这种情况.

谢谢你的帮助!

Ant*_*nes 12

如果它妨碍我们,我们并不总是必须在银色灯光中使用Xaml: -

  public static AnimationHelper
  {
       public static void FadeOutAndCollapse(UIElement target)
       {
           DoubleAnimation da = new DoubleAnimation();
           da.From = 1.0;
           da.To = 0.0;
           da.Duration = TimeSpan.FromSeconds(0.25);
           da.AutoReverse = false;

           StoryBoard.SetTargetProperty(da, new PropertyPath("Opacity"));
           StoryBoard.SetTarget(da, target);

           StoryBoard sb = new StoryBoard();
           sb.Children.Add(da);

           EventHandler eh = null;
           eh = (s, args) =>
           {
                target.Visiblity = Visibility.Collapsed;
                sb.Stop();
                sb.Completed -= eh;
           }
           sb.Completed += eh;

           sb.Begin();
       }
}
Run Code Online (Sandbox Code Playgroud)

有了这个,您可以淡出并折叠任何UI元素: -

AnimationHelper.FadeOutAndCollapse(myTextBox);
Run Code Online (Sandbox Code Playgroud)

我倾向于删除它From = 1.0以使其更通用,以便具有较低起始不透明度的元素在消失之前不会突然闪烁到完全不透明.