这是来自WPF动画新手的两部分问题。首先,这是我的代码:
public void AnimatePaneBox(ContentControl destination,
UIElementCollection source)
{
// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());
Canvas containerCanvas = new Canvas();
int curInteration = 0;
foreach (FrameworkElement element in source)
{
curInteration++;
string iterationName = "iter" + curInteration.ToString();
GeneralTransform tranform;
try
{
// Try to get a transform object. It may not be possible.
// (ie if the source or dest is not docked then it
// will fail and we cannot animate.)
tranform = element.TransformToVisual(destination);
}
catch (InvalidOperationException e)
{
return;
}
Point rootPoint = tranform.Transform(new Point(0, 0));
Rect sourceRelativeRect = new Rect(
rootPoint.X - (destination.ActualWidth / 2),
rootPoint.Y - (destination.ActualHeight / 2),
element.ActualWidth, element.ActualHeight);
RectangleGeometry myRectangleGeometry = new RectangleGeometry
{ Rect = new Rect(0, 0, 0, 0) };
// Assign the geometry a name so that
// it can be targeted by a Storyboard.
RegisterName(iterationName, myRectangleGeometry);
Path myPath = new Path
{
Fill = ((Canvas)element).Background,
StrokeThickness = 1,
Stroke = Brushes.Black,
Data = myRectangleGeometry
};
RectAnimation myRectAnimation = new RectAnimation
{
Duration = TimeSpan.FromSeconds(1),
FillBehavior = FillBehavior.Stop,
AccelerationRatio = 1,
// Set the From and To properties of the animation.
From = sourceRelativeRect,
To = new Rect(0, 0, 0, 0)
};
// Set the animation to target the Rect property
// of the object named "MyAnimatedRectangleGeometry."
Storyboard.SetTargetName(myRectAnimation, iterationName);
Storyboard.SetTargetProperty(myRectAnimation,
new PropertyPath(RectangleGeometry.RectProperty));
// Create a storyboard to apply the animation.
Storyboard ellipseStoryboard = new Storyboard();
ellipseStoryboard.Children.Add(myRectAnimation);
containerCanvas.Children.Add(myPath);
// Start the storyboard when the Path loads.
myPath.Loaded += ((sender, e) => ellipseStoryboard.Begin(this));
}
containerCanvas.Background = Brushes.Transparent;
destination.Content = containerCanvas;
}
这会获取一个对象列表(来自Dotway Zipper Panel)和从它们移动到目的地的动画框。
它工作正常,但要使其工作,我必须覆盖目标的内容。之后我可以恢复它,但我宁愿找到一种方法来完成这项工作而不这样做。我试图创建一个EventTrigger并使用BeginStoryboard对象,但是当我尝试时我根本看不到任何动画。
另外,我很想知道如何让这些动画一次发生一次,而不是一次发生。 (但这是主要问题的次要因素。)
答案 0 :(得分:1)
根据您提供的代码,如果没有内容替换,则无法对其进行动画处理。但是你总是可以在同一个Grid中放置两个控件,比如说......它们会占据同一个地方。一个是动画容器的占位符,另一个是你不想放松的实际内容。
不是在myPath.Loaded += (o, e) => ellipseStoryboard.Begin(this);
处理程序中一起启动它们,而是逐个启动它们。 firstStoryboard.Completed += (o,e)=> nextStoryboard.Begin(this)
;