Wpf storyboard death

WITH#:

public partial class MainWindow : Window { Storyboard a = new Storyboard(); int i; public MainWindow() { InitializeComponent(); a.Completed += new EventHandler(a_Completed); a.Duration = TimeSpan.FromMilliseconds(10); a.Begin(); } void a_Completed(object sender, EventArgs e) { textblock.Text = (++i).ToString(); a.Begin(); } } 

XAML:

 <Window x:Class="Gui.MainWindow" x:Name="control" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="300"> <Canvas> <TextBlock Name="textblock"></TextBlock> </Canvas> 

What is wrong with this code? storyboard stops after 20-50 rounds. Every time a different number

+4
source share
1 answer

I believe, because there is no relationship with your code created between the Storyboard and TextBlock Text DependencyProperty animation clock. if I had to guess, I would say when the Storyboard was going, it was at a somewhat random time due to pollution by the DependencyProperty update stream (TextBlock.Text - DependencyProperty). Creating such an association as shown below (either RunTimeline or RunStoryboard will work, but show alternative search methods):

 public partial class Window1 : Window { Storyboard a = new Storyboard(); StringAnimationUsingKeyFrames timeline = new StringAnimationUsingKeyFrames(); DiscreteStringKeyFrame keyframe = new DiscreteStringKeyFrame(); int i; public Window1() { InitializeComponent(); //RunTimeline(); RunStoryboard(); } private void RunTimeline() { timeline.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(TextBlock.Text)")); timeline.Completed += timeline_Completed; timeline.Duration = new Duration(TimeSpan.FromMilliseconds(10)); textblock.BeginAnimation(TextBlock.TextProperty, timeline); } private void RunStoryboard() { timeline.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(TextBlock.Text)")); a.Children.Add(timeline); a.Completed += a_Completed; a.Duration = new Duration(TimeSpan.FromMilliseconds(10)); a.Begin(textblock); } void timeline_Completed(object sender, EventArgs e) { textblock.Text = (++i).ToString(); textblock.BeginAnimation(TextBlock.TextProperty, timeline); } void a_Completed(object sender, EventArgs e) { textblock.Text = (++i).ToString(); a.Begin(textblock); } } 

This works for me as long as I let him work (~ 10 times longer than ever to win).

Tim

+2
source

All Articles