从右到左动画 window 宽度

Animate window width right to left

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

我找不到关于 window 宽度的任何从右到左的选项,所以我希望在这里找到答案。

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

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

谢谢。

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

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

解决方法是同时为宽度和左侧属性设置动画。这给出了 window 出现在所需位置并向左展开的效果。

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