2013-02-19 74 views
0

我需要通過C#動畫COLLAPSED屬性。如何動畫倒塌的屬性?

我有它下面的代碼幾乎工作正常,除了沒有崩潰動畫。

任何線索?

var myElement = stackObj.Children[n]; 

Duration d = TimeSpan.FromSeconds(2); 
Storyboard sb = new Storyboard() { Duration = d }; 
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d }; 


ObjectAnimationUsingKeyFrames objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames(); 

var discreteObjectKeyFrame = new DiscreteObjectKeyFrame() 
{ 
    KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)) 
}; 

objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame); 

Storyboard.SetTarget(objectAnimationUsingKeyFrames, myElement); 
Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, new PropertyPath("Visibility.Collapsed")); 

sb.Children.Add(DA); 
string myObjectName = "r" + n; 
Storyboard.SetTargetName(DA, myObjectName); 

Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity")); 
sb.Begin(this); 

n++; 

我知道,在XAML應該像

<BeginStoryboard> 
    <Storyboard> 
     <DoubleAnimation 
      Storyboard.TargetProperty = "Opacity" 
      To       = "0" 
      BeginTime     = "0:0:0" 
      Duration     = "0:0:2" /> 
     <ObjectAnimationUsingKeyFrames 
      Storyboard.TargetProperty = "Visibility"> 
      <DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Collapsed}" /> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</BeginStoryboard> 

但我不知道我要實現它的C#代碼。

回答

0

對於需要這種動畫的人,你可以複製/過去的代碼,它的工作! :)

XAML

<StackPanel Background="bLACK" Name="stackObj"> 
      <Rectangle Name="r0" Width="100" Height="50" Fill="#FFDA2121" ></Rectangle> 
      <Rectangle Name="r1" Width="100" Height="50" Fill="#FFDA2121" ></Rectangle> 
      <Rectangle Name="r2" Width="100" Height="50" Fill="#FFD8C828" ></Rectangle> 
      <Rectangle Name="r3" Width="100" Height="50" Fill="#FF17D829" ></Rectangle> 
      <Rectangle Name="r4" Width="100" Height="50" Fill="#FF0DE080" ></Rectangle> 
      <Rectangle Name="r5" Width="100" Height="50" Fill="#FF2E5CF9" ></Rectangle> 
      <Rectangle Name="r6" Height="50" Width="100" Fill="#FFDA2121"/> 
      <Rectangle Name="r7" Height="50" Width="100" Fill="#FFDA2121"/> 
      <Rectangle Name="r8" Height="50" Width="100" Fill="#FFDA2121"/> 
     </StackPanel> 

C#

public partial class MainWindow : Window 
    { 
     DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Background); 

     public MainWindow() 
     { 
      InitializeComponent(); 
      Loaded += MainWindow_Loaded; 
     } 

     void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      timer.Interval = TimeSpan.FromSeconds(3); 
      timer.Tick += timer_Tick; 
      timer.Start(); 

      sb = new Storyboard(); 
     } 
     int n = 0; 
     bool isWorking; 
     Storyboard sb; 
     void timer_Tick(object sender, EventArgs e) 
     { 
      if (isWorking == false) 
      { 
       isWorking = true; 
       try 
       { 
        var myElement = stackObj.Children[n]; 

        var dur = TimeSpan.FromMilliseconds(2000); 

        var f = CreateVisibility(dur, myElement, false); 

        sb.Children.Add(f); 

        Duration d = TimeSpan.FromSeconds(2); 
        DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d }; 

        sb.Children.Add(DA); 
        string myObjectName = "r" + n; 
        Storyboard.SetTargetName(DA, myObjectName); 

        Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity")); 

        sb.Begin(this); 

        n++; 
       } 
       catch (Exception ex) 
       { 
        Debug.WriteLine(ex.Message + " " + DateTime.Now.TimeOfDay); 
       } 

       isWorking = false; 
      } 
     } 

     private static ObjectAnimationUsingKeyFrames CreateVisibility(Duration duration, UIElement element, bool show) 
     { 
      var _Two = new DiscreteObjectKeyFrame { KeyTime = new TimeSpan(duration.TimeSpan.Ticks/2), Value = (show ? Visibility.Visible : Visibility.Collapsed) }; 

      var _Animation = new ObjectAnimationUsingKeyFrames { BeginTime = new TimeSpan(0) }; 
      _Animation.KeyFrames.Add(_Two); 
      Storyboard.SetTargetProperty(_Animation, new PropertyPath("Visibility")); 
      Storyboard.SetTarget(_Animation, element); 
      return _Animation; 
     } 

    }