动画窗口宽度从右到左

时间:2015-06-17 05:25:28

标签: c# wpf animation storyboard

我正在制作侧边栏式应用程序,它位于屏幕右侧的左侧。 我试图让它看起来像是滑入和滑出屏幕,但当它在右边时,它从左侧滑入。

我找不到任何关于窗口宽度的权利,所以我希望在这里找到答案。

这是我从左到右的当前工作动画代码:

    public static void AnimateSize(this Window target, double newWidth, EventHandler completed)
    {
        var length = new TimeSpan(0, 0, 0, 0, Settings.Default.AnimationTime);
        var sb = new Storyboard {Duration = new Duration(length)};

        var aniWidth = new DoubleAnimationUsingKeyFrames();

        aniWidth.Duration = new Duration(length);

        aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(target.ActualWidth,
            KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 00))));
        aniWidth.KeyFrames.Add(new EasingDoubleKeyFrame(newWidth, KeyTime.FromTimeSpan(length)));

        Storyboard.SetTarget(aniWidth, target);
        Storyboard.SetTargetProperty(aniWidth, new PropertyPath(FrameworkElement.WidthProperty));

        sb.Children.Add(aniWidth);

        sb.Completed += completed;
        sb.Begin();
    }

这样叫:

_window.AnimateSize(0, delegate { _window.Hide(); });

和此:

_window.Width = 0;
_window.Show();
_window.AnimateSize(Settings.Default.Width, delegate { });

感谢。

2 个答案:

答案 0 :(得分:2)

        private void Animate(double beginfrom, double to, DependencyProperty dp)
        {
            var da = new DoubleAnimation {
                From = beginfrom,
                To = to,
                FillBehavior = FillBehavior.Stop,
                Duration = new Duration(TimeSpan.FromSeconds(0.3)),
                AccelerationRatio = 0.1
            };
            var storyBoard = new Storyboard();
            storyBoard.Children.Add(da);
            Storyboard.SetTarget(da, this); //this = your control object
            Storyboard.SetTargetProperty(da, new PropertyPath(dp));
            storyBoard.Begin();
        }

简单用法:Animate(0, 250, WidthProperty);

答案 1 :(得分:1)

我有类似的任务要完成。我曾经为我的窗口的左侧属性设置动画,使其看起来好像从外面滑动并以相同的方式消失。但是当使用两个屏幕的同事使用它时,窗口会滑到另一个屏幕上。

使用的工作是同时为宽度和左边属性设置动画。这样可以看到窗口出现在所需位置并向左侧展开。

DoubleAnimation daLeftAppear = new DoubleAnimation(popupWidthDetails.workAreaWidth, (<workAreaWidth> - <windowWidth> - 5), new Duration(TimeSpan.FromSeconds(1)));
        daLeftAppear.EasingFunction = new QuarticEase();
        Storyboard.SetTargetName(daLeftAppear, this.Name);
        Storyboard.SetTargetProperty(daLeftAppear, new PropertyPath(LeftProperty));

        DoubleAnimation daWidthAppear = new DoubleAnimation(0, 100, new Duration(TimeSpan.FromSeconds(1)));
        daWidthAppear.EasingFunction = new QuarticEase();
        Storyboard.SetTargetName(daWidthAppear, this.Name);
        Storyboard.SetTargetProperty(daWidthAppear, new PropertyPath(WidthProperty));

        sbAppear = new Storyboard();
        sbAppear.Children.Add(daLeftAppear);
        sbAppear.Children.Add(daWidthAppear);