Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置折叠属性的动画?_C#_.net_Wpf_Animation - Fatal编程技术网

C# 如何设置折叠属性的动画?

C# 如何设置折叠属性的动画?,c#,.net,wpf,animation,C#,.net,Wpf,Animation,我需要通过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

我需要通过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代码。

对于需要这种动画的人,你可以复制/复制过去的代码,并且它可以工作!:)

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

    }