Wait for animation, render to complete - XAML and C#

三世轮回 提交于 2021-02-05 04:46:10

问题


I have a situation where I am animating part of my XAML application, and I need to wait for the animation AND rendering to complete before I can move on in my code. So far the tail end of my function looks like:

    ProcExpandCollapse.Begin();
    while (ProcExpandCollapse.GetCurrentState() != ClockState.Stopped) { }
}

Which, in theory, will wait until the animation is finished. But it will not wait until the rendering is finished - the thread drawing the application might still not have re-drawn the animation.

The animation is expanding a UIElement, and then the next part of my code uses it's rendered size to do some things. My question then is, how do I wait until my UI Element is re-rendered before moving on?


回答1:


I finally found an answer (although I had to pay for consulting services)! Essentially what I ended up doing was putting the bit of code which uses the rendered controls on the control dispatcher's queue with very low priority, so that it naturally renders before handling that task. For example:

mycontrol.Dispatcher.BeginInvoke((Action)delegate{textbox1.Text = "Grazie";});
mycontrol.Dispatcher.Invoke((Action)delegate{GetScreenshot();}, DispatcherPriority.Background);

The code will then procede after GetScreenshot is called and finished, which will be after the rendering is completed (because rendering has higher priority than background).




回答2:


The Storyboard object has a 'Completed' event you can subscribe to. It will be called when the animation is done. This what you are looking for?

Basically I think you want "ProcExpandCollapse.Completed +=" (then hit tab a view times, VS will generate a snippet for you)



来源:https://stackoverflow.com/questions/2990373/wait-for-animation-render-to-complete-xaml-and-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!