実行時に作成された n 個のオブジェクトにフェードインを適用するコード ビハインドを使用して、単純なダブル アニメーションを追加したいと考えています。
foreach (var rect in howmanyrect) {
Rectangle bbox = new Rectangle {
Width = rect.Width,
Height = rect.Height,
Stroke = Brushes.BlueViolet,
Opacity = 0D
};
DoubleAnimation da = new DoubleAnimation {
From = 0D,
To = 1D,
Duration = TimeSpan.FromMilliseconds(500D),
RepeatBehavior = new RepeatBehavior(1),
AutoReverse = false
};
GridContainer.Children.Add(bbox);
Canvas.SetLeft(bbox, rect.Left);
Canvas.SetTop(bbox, rect.Top);
bbox.Tag = da; // <- Look HERE
bbox.BeginAnimation(OpacityProperty, da);
この後、要求された場合は、オブジェクト コレクションをフェードアウトで削除します。
foreach (var child in GridContainer.Children) {
Rectangle bbox = (Rectangle) child;
DoubleAnimation da = (DoubleAnimation) bbox.Tag; // <- Look HERE
da.From = 1D;
da.To = 0D;
var childCopy = child; // This copy grants the object reference access for removing inside a forech statement
da.Completed += (obj, arg) => viewerGrid.Children.Remove((UIElement) childCopy);
bbox.BeginAnimation(OpacityProperty, da);
}
このコードは完全に機能しますが、これは回避策です。私の最初のリビジョンでは、delete メソッドで新しい Doubleanimation オブジェクトを作成しましたが、アニメーションを開始すると、すべてのオブジェクトが最初と 2 番目のアニメーションを実行してから削除されました。そこで、Tag プロパティを使用して DoubleAnimation インスタンスへの参照を渡し、アニメーション プロパティを変更することにしました。
BeginAnimation にアタッチされた DoubleAnimation オブジェクトへの参照を取得する、または最初のアニメーションが繰り返されないようにする別の方法はありますか?
ありがとうロックス