Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 762
Adam S Avatar asked Jun 07 '10 14:06

Adam S


1 Answers

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).

like image 104
Adam S Avatar answered Sep 22 '22 01:09

Adam S