我正在使用Viewport3D编写一个3D wpf应用程序。当用户按下按钮时,我必须在AxisAngleRotation3D
上启动DoubleAnimation
,但它必须同步完成。我无法在animation.Completed
上执行此操作,因为此动画以递归方式运行下一个和下一个。WPF同步动画和UI线程死锁
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();
是单一方法中的代码? –
是的。 @ d.moncada – Mateusz