ユーザーの入力に基づいてアニメーション化するシミュレーション プログラムを作成しようとしています。mystoryboard.completed イベントのイベント ハンドラを作成しようとすると、エラーが発生します。イベント処理とストーリーボードに関する多数の異なる API 記事とフォーラム投稿を読みましたが、エラーの原因を見つけることができないようです。
コードがコンパイルされ、ウィンドウが表示されますが、イベント ハンドラを設定した行より後の行は実行されません。すべてを設定した 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 が作成された行に到達すると、残りのコードの実行を停止し、ウィンドウをそのままロードします。なぜこれが起こっているのか、一生理解できません。