我最終只是通過在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?相反,使用顯式空檢查而不是查詢。
你能舉一個它如何使用的例子嗎? –
我有點不清楚如何在WPF項目中使用blend的sdk。當我嘗試打開視覺工作室的混合時,我甚至無法創建一個新項目(新項目對話框的確定按鈕什麼都不做)... –