我不知道如何禁用自動滾動動作,但我更喜歡的代碼速戰速決:
在課堂ItemFlyInAndOutAnimations
添加一個字段
private FrameworkElement _element; //let's consider the line is 266
in public void ItemFlyIn()
進行更改:
public void ItemFlyIn()
{
if (_popupCanvas.Children.Count != 2)
return;
_popup.IsOpen = true;
_backgroundMask.Opacity = 0.0;
Image animatedImage = _popupCanvas.Children[1] as Image;
var sb = new Storyboard();
var rootFame = Application.Current.RootVisual as FrameworkElement; //new line
var targetElementPosition = _element.GetRelativePosition(rootFame); //new line
// animate the X position
var db = CreateDoubleAnimation(targetElementPosition.X - 100, targetElementPosition.X,
new SineEase(),
_targetElementClone, Canvas.LeftProperty, _flyInSpeed); //reference changed!
sb.Children.Add(db);
// animate the Y position
db = CreateDoubleAnimation(targetElementPosition.Y - 50, targetElementPosition.Y,
new SineEase(),
_targetElementClone, Canvas.TopProperty, _flyInSpeed); //reference changed!
sb.Children.Add(db);
//other code is the same
在public void ItemFlyOut(FrameworkElement element, Action action)
此行之後
_targetElementPosition = element.GetRelativePosition(rootElement);
加上這樣一條:
_element = element;
我做了什麼:
在這段代碼中我保存參考動畫UI元素並更新它的位置n在後面動畫。
你最好測試這段代碼,但它似乎沒問題。
非常好。解決問題! – AndrewJE