Storyboard.Completed事件处理程序防止执行代码

时间:2014-09-03 13:24:12

标签: c# wpf storyboard eventhandler

我正在尝试创建一个基于用户输入动画的模拟程序。当我尝试为mystoryboard.completed事件创建一个事件处理程序时,我遇到了一个错误。我在事件处理和故事板上阅读了许多不同的API文章和论坛帖子,但我似乎无法找到错误的原因。

我的代码编译并且窗口显示但是在我设置eventhandler的行之后的任何内容都没有被执行。我在其中设置所有内容的MainWindow如下所示。

public MainWindow()
        {
            InitializeComponent();
            titleTextBlock.Text = "MainWindow()";
            //this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            mainSystem = new BalanceSystem(3);
            leftBlock = new SystemComponents.Block(0, 100, 150, 100, mainSystem);
            rightBlock = new SystemComponents.Block(1, 100, 150, 100, mainSystem);
            balanceBeam = new SystemComponents.Bar(0, 0, 250, 150, 100, mainSystem);
            mainSystem.addComponent(leftBlock, leftWeight);
            mainSystem.addComponent(rightBlock, rightWeight);
            mainSystem.addComponent(balanceBeam, balanceBar);

            titleTextBlock.Text = "LOADED";
        }

“BalanceSystem”的构造函数是事情开始出错的时候。它进入下面显示的构造函数:

public BalanceSystem(int count)
{
    componentCount = count;
    masterTimeline = new MovementTimeline(1);
}

在输入“BalanceSystem”的构造函数后,它会转到我的客户类“MovementTimeline”的构造函数。打破一切的行是masterStoryboard.Completed的事件处理程序的创建和订阅。

class MovementTimeline
{
    private Storyboard masterStoryboard;
    private Duration systemDuration;

    public MovementTimeline(int totalTime)
    {
        systemDuration = new Duration(TimeSpan.FromSeconds(totalTime));
        masterStoryboard.Completed += new EventHandler(masterStoryboard_Completed);

    }

    void masterStoryboard_Completed(object sender, EventArgs e)
    {
        masterStoryboard.Children.Clear();
        //masterStoryboard.Completed -= masterStoryboard_Completed;
    }
 }

一旦编译器或程序到达创建新EventHandler的行,它就会停止执行我的其余代码并按原样加载窗口。我不能为我的生活弄清楚为什么会这样。

1 个答案:

答案 0 :(得分:0)

它让我觉得你在没有创建StoryBoard对象的情况下添加了一个事件处理程序

相关问题