我正在使用Viewport3D编写3D wpf应用程序.当用户按下一个按钮,我必须开始DoubleAnimation
上AxisAngleRotation3D
,但必须做到同步.我不能这样做animation.Completed
,因为这个动画递归地运行下一个和下一个动画.
ButtonHandler必须在UI线程上工作,因为要计算动画我几乎不使用Viewport3D.
所以我必须在UI线程中启动同步动画并在完成后继续工作.
我尝试了这段代码,但它会导致死锁:
AxisAngleRotation3D axis;
DoubleAnimation animation;
DependencyProperty dp;
var worker = new Thread(() =>
{
Dispatcher.CurrentDispatcher.InvokeShutdown();
Dispatcher.Run();
});
worker.SetApartmentState(ApartmentState.STA);
worker.Start();
AutoResetEvent animationDone = new AutoResetEvent(false);
EventHandler handler = null;
handler = (sender, args) =>
{
animation.Completed -= handler; // remove self
animationDone.Set(); // signal completion
};
animation.Completed += handler;
Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
{
axis.BeginAnimation(dp, animation);
}));
animationDone.WaitOne();
Run Code Online (Sandbox Code Playgroud)