WPF死亡故事板 [英] wpf storyboard death
问题描述
C#:
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:
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>
什么是错的代码?后两轮20-50
情节提要停止。每一次不同的号码
What is wrong with this code? the storyboard stops after 20-50 rounds. Every time a different number
推荐答案
我相信这是因为你的代码有没有故事板的动画时钟之间建立关系的TextBlock的文本的DependencyProperty。如果我猜当故事板是conking了,我会说,它是在一个有些随机时间,由于积垢的DependencyProperty(TextBlock.Text是一个的DependencyProperty)更新管道。下面创建这样一个关联关系(无论RunTimeline或RunStoryboard的工作,但显示的看着这个替代方法):
I believe it's because with your code there is no relation created between the Storyboard's animation clock and the TextBlock's Text DependencyProperty. if I had to guess I would say when the Storyboard was conking out, it was at a somewhat random time due to fouling the DependencyProperty (TextBlock.Text is a DependencyProperty) update pipeline. Creating such an association as below (either RunTimeline or RunStoryboard will work, but show alternate methods of looking at this):
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);
}
}
这只要我会让工作对我来说它运行(〜长于以往任何时候都走上否则发生故障10次)。
This works for me for as long as I would let it run (~10 times longer than it ever took to conk out otherwise).
添
这篇关于WPF死亡故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!