0

在 Windows 商店应用程序中,可以将 RepositionThemeTransition 添加到 UI 元素,以便在它们的位置发生变化时使它们俯冲而不是传送。还有其他类型的过渡。

<Rectangle>
    <Rectangle.Transitions>
        <TransitionCollection>
            <RepositionThemeTransition/>
        </TransitionCollection>
    </Rectangle>
</Rectangle>

WPF 似乎不支持此功能。有没有办法做等效的事情?

4

2 回答 2

0

我认为您可能正在寻找的是Blend 的 SDKFluidMoveBehavior中的行为。网络上有几个教程可用 -如果您不熟悉行为或此特定行为,这里有一个。

于 2012-11-21T23:06:17.190 回答
0

我最终只是自己模拟了该功能,通过在最初抵消位置变化的 LayoutUpdated 事件中应用动画。这是代码:

public static void AddRepositionTransitionsUsingRenderTransform(this FrameworkElement control, bool x = true, bool y = true, CancellationToken lifetime = default(CancellationToken)) {
    if (control == null) throw new ArgumentNullException("control");
    var approachPeriod = TimeSpan.FromMilliseconds(100);

    // animate when positions change, to give a 'swooping' effect instead of teleporting
    var transform = new TranslateTransform();
    var oldPosition = May<Point>.NoValue;
    EventHandler updated = (sender, arg) => {
        // determine where the control has moved to
        var newPosition = control.ActualWidth == 0 || control.ActualHeight == 0 || control.Visibility != Visibility.Visible
                        ? May<Point>.NoValue
                        : control.TranslatePoint(new Point(-transform.X, -transform.Y), Application.Current.MainWindow);
        if (oldPosition == newPosition) return;

        // adjust the animation to initially cancel the change in position, and finish at the new final position after the approach period
        var dif = (from o in oldPosition
                    from n in newPosition
                    select new Point(n.X - o.X, n.Y - o.Y)
                    ).ElseDefault();
        if (x) transform.BeginAnimation(TranslateTransform.XProperty, new DoubleAnimation(transform.X - dif.X, 0, approachPeriod));
        if (y) transform.BeginAnimation(TranslateTransform.YProperty, new DoubleAnimation(transform.Y - dif.Y, 0, approachPeriod));

        oldPosition = newPosition;
    };

    // register for events and replace transform, until lifetime ends
    var oldTransform = control.RenderTransform;
    control.RenderTransform = transform;
    control.LayoutUpdated += updated;
    lifetime.Register(() => {
        control.LayoutUpdated -= updated;
        control.RenderTransform = oldTransform;
    });
}

请注意,“May”是自定义选项类型,因此该部分不会编译。你可以使用类型点吗?相反,使用显式空检查而不是查询。

于 2012-11-22T19:27:55.737 回答