Tôi đang tìm hiểu về hoạt ảnh WPF và bối rối về cách áp dụng tuần tự hình động. Như một ví dụ đơn giản, tôi đã có bốn hình chữ nhật trong một lưới thống nhất, và muốn thay đổi màu sắc của mỗi một tuần tự. Dưới đây là những gì tôi có cho đến thời điểm này:WPF - hoạt ảnh tuần tự ví dụ đơn giản
public partial class Window1 : Window
{
Rectangle blueRect;
Rectangle redRect;
Rectangle greenRect;
Rectangle yellowRect;
public Window1()
{
InitializeComponent();
blueRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Blue, Name="Blue"};
redRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Red, Name="Yellow"};
greenRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Green, Name="Green" };
yellowRect = new Rectangle() { Fill = System.Windows.Media.Brushes.Yellow, Name="Yellow" };
UniformGrid1.Children.Add(blueRect);
UniformGrid1.Children.Add(redRect);
UniformGrid1.Children.Add(greenRect);
UniformGrid1.Children.Add(yellowRect);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
animateCell(blueRect, Colors.Blue);
animateCell(redRect, Colors.Red);
}
private void animateCell(Rectangle rectangle, Color fromColor)
{
Color toColor = Colors.White;
ColorAnimation ani = new ColorAnimation(toColor, new Duration(TimeSpan.FromMilliseconds(300)));
ani.AutoReverse = true;
SolidColorBrush newBrush = new SolidColorBrush(fromColor);
ani.BeginTime = TimeSpan.FromSeconds(2);
rectangle.Fill = newBrush;
newBrush.BeginAnimation(SolidColorBrush.ColorProperty, ani);
//NameScope.GetNameScope(this).RegisterName(rectangle.Name, rectangle);
//Storyboard board = new Storyboard();
//board.Children.Add(ani);
//Storyboard.SetTargetName(rectangle, rectangle.Name);
//Storyboard.SetTargetProperty(ani, new PropertyPath(SolidColorBrush.ColorProperty));
//board.Begin();
}
Cách dễ nhất để hoàn thành việc này là gì? Mã trong các ý kiến là dự đoán đầu tiên của tôi, nhưng nó không hoạt động chính xác.
Điều gì xảy ra nếu hoạt ảnh động? Tôi muốn có thể gọi là màu xanh, xanh, đỏ, xanh dương, xanh lục, đỏ; mỗi hoạt ảnh có thể 2 giây sau đó. Có cách nào để làm cho người gọi của animateCell khối cho đến khi ani.Completed được bắn? –