微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – wpf故事板死亡

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轮之后停止.每次都有不同的号码

解决方法

我相信这是因为你的代码在Storyboard的动画时钟和TextBlock的Text DependencyProperty之间没有创建任何关系.如果我不得不猜测我会说故事板何时出来,那么由于污染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倍).

蒂姆

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐