2012-11-21 69 views
0

在windows应用商店中,可以将RepositionThemeTransition添加到UI元素中,以便在它们的位置发生变化时能够立即使用,而不是传送。还有其他类型的转换。RepositionThemeTransition等价于WPF

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

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

回答

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

需要注意的是“可以”自定义选项类型,所以这部分将不会编译。你可以使用类型Point?相反,使用显式空检查而不是查询。

0

我想你可能要找的是FluidMoveBehavior行为Blend's SDK。网上有几个教程 - here's one以防您不熟悉行为或此特定行为。

+0

你能举一个它如何使用的例子吗? –

+0

我有点不清楚如何在WPF项目中使用blend的sdk。当我尝试打开视觉工作室的混合时,我甚至无法创建一个新项目(新项目对话框的确定​​按钮什么都不做)... –