加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – wpf故事板死亡

发布时间:2020-12-15 22:56:19 所属栏目:百科 来源:网络整理
导读: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
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倍).

蒂姆

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读