我有以下代码:
private void Package_ContactDown(object sender, ContactEventArgs e)
{
ScatterViewItem svi = new ScatterViewItem();
svi.Orientation = 0;
removeShadow(svi);
svi.IsActive = true;
PackageView view = new PackageView(sourceFile, this);
view.setScatterViewItem(svi);
svi.Width = 1024;
svi.Height = 768;
svi.Center = new Point(512, 384);
Viewbox box = new Viewbox();
box.Name = "box";
box.Child = view;
this.RegisterName(box.Name, box);
Viewbox boxSmall = new Viewbox();
boxSmall.Name = "boxSmall";
this.RegisterName(boxSmall.Name, boxSmall);
TextBlock txt = new TextBlock();
txt.Foreground = Brushes.White;
txt.Text = "Package of class";
boxSmall.Child = txt;
boxSmall.Opacity = 0;
boxSmall.IsHitTestVisible = false;
Rectangle border = new Rectangle();
border.Name = "border";
this.RegisterName(border.Name, border);
border.Fill = Brushes.Transparent;
border.Stroke = Brushes.White;
border.StrokeThickness = 2;
border.Opacity = 0;
Grid g = new Grid();
g.Background = this.FindResource("WindowBackground") as ImageBrush;
g.Children.Add(box);
g.Children.Add(boxSmall);
g.Children.Add(border);
svi.Content = g;
window.IconDisplay.Items.Add(svi);
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0.0;
animation.To = 1.0;
animation.Duration = new Duration(TimeSpan.FromSeconds(3));
animation.AutoReverse = false;
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(animation);
Storyboard.SetTargetName(animation, boxSmall.Name);
Storyboard.SetTargetProperty(animation, new PropertyPath(Viewbox.OpacityProperty));
DoubleAnimation animation2 = new DoubleAnimation();
animation2.From = 1.0;
animation2.To = 0.0;
animation2.Duration = new Duration(TimeSpan.FromSeconds(3));
animation2.AutoReverse = false;
Storyboard storyboard2 = new Storyboard();
storyboard2.Children.Add(animation2);
Storyboard.SetTargetName(animation2, box.Name);
Storyboard.SetTargetProperty(animation2, new PropertyPath(Viewbox.OpacityProperty));
DoubleAnimation animation3 = new DoubleAnimation();
animation3.From = 0.0;
animation3.To = 1.0;
animation3.Duration = new Duration(TimeSpan.FromSeconds(3));
animation3.AutoReverse = false;
Storyboard storyboard3 = new Storyboard();
storyboard3.Children.Add(animation3);
Storyboard.SetTargetName(animation3, border.Name);
Storyboard.SetTargetProperty(animation3, new PropertyPath(Rectangle.OpacityProperty));
svi.SizeChanged += delegate(object s, SizeChangedEventArgs args)
{
if (args.NewSize.Width < 150 && args.NewSize.Height < 150 && !isSmall)
{
svi.CanScale = false;
storyboard.Begin(this);
storyboard2.Begin(this);
storyboard3.Begin(this);
storyboard3.Completed += delegate(object sender2, EventArgs args2)
{
Console.WriteLine("Storyboard completed");
svi.CanScale = true;
};
isSmall = true;
}
if (args.NewSize.Width > 150 && args.NewSize.Height > 150 && isSmall)
{
isSmall = false;
}
};
}
我注意到Storyboard#completed事件永远不会被触发。为什么?还有一个问题......有没有办法扭转所有这3个动画?如果我想以相反的方式显示动画?
答案 0 :(得分:2)
完成的事件不会在第一次触发,因为在调用begin方法之前未设置它。设置完成的处理程序,然后调用begin,你应该看到处理程序被调用。
你有三个故事板的原因吗?故事板可以包含多个动画,您可以将所有动画放入一个故事板中。这将简化故事板的逆转。
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0.0;
animation.To = 1.0;
animation.Duration = new Duration(TimeSpan.FromSeconds(3));
animation.AutoReverse = false;
DoubleAnimation animation2 = new DoubleAnimation();
animation2.From = 1.0;
animation2.To = 0.0;
animation2.Duration = new Duration(TimeSpan.FromSeconds(3));
animation2.AutoReverse = false;
DoubleAnimation animation3 = new DoubleAnimation();
animation3.From = 0.0;
animation3.To = 1.0;
animation3.Duration = new Duration(TimeSpan.FromSeconds(3));
animation3.AutoReverse = false;
Storyboard storyboard = new Storyboard();
storyboard.AutoReverse = true;
storyboard.Children.Add(animation);
Storyboard.SetTargetName(animation, boxSmall.Name);
Storyboard.SetTargetProperty(animation, new PropertyPath(Viewbox.OpacityProperty));
storyboard.Children.Add(animation2);
Storyboard.SetTargetName(animation2, box.Name);
Storyboard.SetTargetProperty(animation2, new PropertyPath(Viewbox.OpacityProperty));
storyboard.Children.Add(animation3);
Storyboard.SetTargetName(animation3, border.Name);
Storyboard.SetTargetProperty(animation3, new PropertyPath(Rectangle.OpacityProperty));
svi.SizeChanged += delegate(object s, SizeChangedEventArgs args)
{
if (args.NewSize.Width < 150 && args.NewSize.Height < 150 && !isSmall)
{
svi.CanScale = false;
storyboard.Completed += (o, s) =>
{
Console.WriteLine("Storyboard completed");
svi.CanScale = true;
};
storyboard.Begin(this);
isSmall = true;
}
if (args.NewSize.Width > 150 && args.NewSize.Height > 150 && isSmall)
{
isSmall = false;
}
};