【问题标题】:WPF: Measure/Arrange gets called only once during animationWPF:在动画期间仅调用一次测量/排列
【发布时间】:2014-01-19 11:34:03
【问题描述】:

我正在开发一个自定义的类似网格的面板,其中一个元素可以缩放。 首先,所有元素都具有相同的大小。如果一个应该缩放,它会得到两倍于以前的大小,其余元素均匀地填充剩余空间(因此,除了与缩放元素在同一行或列中的元素之外,所有元素的高度和宽度都会变小,在同一行得到更小的宽度和缩放的高度等)。

在我为缩放添加动画之前一切正常。

我正在计算测量过程中每个元素的大小,起初我用计算出的大小来测量每个元素。元素本身的大小为 NaN,直到它们被缩放。我通过将它们设置为最终大小来缩放它们,然后开始从旧到新的双重动画。

但是,当我添加动画时,仅在第一个动画步骤中调用了测量和排列。 当我尝试使用 availableSize 或无限大小来测量元素时,动画效果很好,但元素没有正确调整尺寸,因为它们变小了。

有谁知道如何解决其中任何一种行为?

问候 托比亚斯

编辑:动画代码:

        Size normalSize = GetNormalElementSize(new Size(ActualWidth, ActualHeight));

        DoubleAnimation widthAnimation = new DoubleAnimation
        {
            From = normalSize.Width,
            To = normalSize.Width * 2,
            Duration = TimeSpan.FromMilliseconds(750),
            EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseInOut }
        };

        DoubleAnimation heightAnimation = new DoubleAnimation
        {
            From = normalSize.Height,
            To = normalSize.Height * 2,
            Duration = TimeSpan.FromMilliseconds(750),
            EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseInOut }
        };

        ZoomedElement.Height = normalSize.Height;
        ZoomedElement.Width = normalSize.Width;

        Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(FrameworkElement.WidthProperty));
        Storyboard.SetTarget(widthAnimation, ZoomedElement);
        Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
        Storyboard.SetTarget(heightAnimation, ZoomedElement);

        Storyboard zoomStoryboard = new Storyboard();
        zoomStoryboard.Children.Add(heightAnimation);
        zoomStoryboard.Children.Add(widthAnimation);
        zoomStoryboard.CurrentTimeInvalidated += zoomStoryboard_CurrentTimeInvalidated;
        zoomStoryboard.Begin();

【问题讨论】:

  • 把那个添加动画的代码放上去,对你有帮助。
  • 我在上面添加了我正在使用的代码

标签: wpf layout wpf-controls measureoverride arrangeoverride


【解决方案1】:

我猜你正在使用 RenderTransformation,试试 LayoutTransformation。

【讨论】:

  • 见我上面的编辑。我尝试将 ScaleTransform 设置为 Layouttransform,最终我的缩放元素想要全屏作为大小(我猜是因为它们在缩放之前具有 NaN 大小)而不是它们的实际大小的两倍
猜你喜欢
  • 2011-01-20
  • 1970-01-01
  • 2022-07-06
  • 2021-11-14
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
相关资源
最近更新 更多