WPF中RepositionThemeTransition的等价物

时间:2012-11-21 22:40:47

标签: c# .net wpf layout windows-store-apps

在Windows应用商店应用中,可以将一个RepositionThemeTransition添加到UI元素中,以便在它们的位置发生变化时突然移动而不是传送。还有其他类型的过渡。

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

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

2 个答案:

答案 0 :(得分:0)

我认为您可能正在寻找的是Blend's SDK中的FluidMoveBehavior行为。网上有几个教程 - here's one,以防您不熟悉行为或此特定行为。

答案 1 :(得分: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”是自定义选项类型,因此该部分将无法编译。你可以使用Point类型吗?相反,使用显式空检查而不是查询。