【问题标题】:wpf storyboard deathwpf故事板死亡
【发布时间】:2010-09-23 20:18:06
【问题描述】:

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:

<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 轮后停止。每次都是不同的数字

【问题讨论】:

  • 非常有趣的问题,当我的鼠标移到 TextBlock 上时,我发现这个停止了,有时我会得到 1500。

标签: c# wpf xaml timer storyboard


【解决方案1】:

我相信这是因为您的代码没有在 Storyboard 的动画时钟和 TextBlock 的 Text DependencyProperty 之间创建任何关系。如果我不得不猜测,我会说 Storyboard 何时结束,这是由于 DependencyProperty (TextBlock.Text 是一个 DependencyProperty)更新管道的污染而发生的随机时间。创建如下这样的关联(RunTimeline 或 RunStoryboard 都可以,但显示查看此关联的替代方法):

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 倍)。

提姆

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多