WPF正在等待UI完成
我有一个代码需要刷新UI,然后等待它完成(刷新可能涉及动画),然后继续。是否有方法以同步方式调用WPF正在等待UI完成,wpf,user-interface,thread-safety,Wpf,User Interface,Thread Safety,我有一个代码需要刷新UI,然后等待它完成(刷新可能涉及动画),然后继续。是否有方法以同步方式调用应用程序.Current.Dispatcher.Invoke(新操作(()=>{PerformUpdate WithAnimations();} 这是整体外观: List<thingsThatMove> myThings = new List<ThingsThatMove>(); //normal code interacting with the data // let's
应用程序.Current.Dispatcher.Invoke(新操作(()=>{PerformUpdate WithAnimations();}
这是整体外观:
List<thingsThatMove> myThings = new List<ThingsThatMove>();
//normal code interacting with the data
// let's name this part of code A
foreach (thing t in myThings)
{
thing.currentPosition = SomePoint;
if(thing.wasRejectedBySystem) thing.needsToMove = true;
}
//As a result of A we have some impact in the UI
//That may need some animations (let's call this bloc B)
Application.Current.Dispatcher.Invoke(new Action(() => {
foreach(thing in myThings)
if(thing.needsToMove)
createNewAnimation(thing);
}));
//Here is some final code that needs the final position of some
//of the elements, so it cannot be executed until the B part has
// been finished. Let's call this bloc C
updateInternalValues(myThings);
cleanUp();
List myThings=new List();
//与数据交互的普通代码
//让我们把这部分代码命名为A
foreach(神话中的事物)
{
thing.currentPosition=某个点;
如果(thing.wassedbysystem)thing.needsToMove=true;
}
//作为一个结果,我们在UI中有一些影响
//这可能需要一些动画(我们称之为B组)
Application.Current.Dispatcher.Invoke(新操作(()=>{
foreach(神话中的事物)
如果(东西需要移动)
创作新动画(东西);
}));
//下面是一些最终代码,需要一些
//元素的一部分,因此在B部分完成之前无法执行
//已经结束了,我们把这个集团称为
更新内部值(神话);
清理();
我尝试将B封装到Backgroundroker中。将B bloc设置为DoWork
并侦听completed
,但它不起作用,因为B blok在Application.Current.Dispatcher调用后“完成”,而不是在Dispatcher本身完成所有操作后
如何使C等待直到B中的所有动画完成?您可以使用async/await以异步方式调用,并在执行后继续
private async void RefreshCode()
{
List<thingsThatMove> myThings = new List<ThingsThatMove>();
//normal code interacting with the data
// let's name this part of code A
foreach (thing t in myThings)
{
thing.currentPosition = SomePoint;
if(thing.wasRejectedBySystem) thing.needsToMove = true;
}
//As a result of A we have some impact in the UI
//That may need some animations (let's call this bloc B)
await Application.Current.Dispatcher.InvokeAsync(new Action(() => {
foreach(thing in myThings)
if(thing.needsToMove)
createNewAnimation(thing);
}));
//Here is some final code that needs the final position of some
//of the elements, so it cannot be executed until the B part has
// been finished. Let's call this bloc C
updateInternalValues(myThings);
cleanUp();
}
private async void RefreshCode()
{
列表神话=新列表();
//与数据交互的普通代码
//让我们把这部分代码命名为A
foreach(神话中的事物)
{
thing.currentPosition=某个点;
如果(thing.wassedbysystem)thing.needsToMove=true;
}
//作为一个结果,我们在UI中有一些影响
//这可能需要一些动画(我们称之为B组)
等待Application.Current.Dispatcher.InvokeAsync(新操作(()=>{
foreach(神话中的事物)
如果(东西需要移动)
创作新动画(东西);
}));
//下面是一些最终代码,需要一些
//元素的一部分,因此在B部分完成之前无法执行
//已经结束了,我们把这个集团称为
更新内部值(神话);
清理();
}
如果它异步启动一个委托…我的代码不需要等待它完成就可以继续运行吗?我需要等待它完成,然后再继续运行。有没有办法将UI级别添加到任务中?因为在我的PerformUpdate WithAnimations中,我有一个对Application.dispatcher.invokeI的调用,需要查看更多代码。发布w的示例我用大量的伪代码对问题进行了重大更新,以澄清问题。