0

我需要COLLAPSED通过 C# 为属性设置动画。

我有下面的代码,除了没有折叠动画外,它几乎可以正常工作。

有什么线索吗?

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# 代码。

4

1 回答 1

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;
        }

    }
于 2013-02-19T17:07:03.560 回答