When I add many items to Canvas why does the UI freeze? Say I have a loop from 1 to 1000. I can only see all the elements after all 1000 of them are added. Why doesn't the Canvas show up items as they are added or removed one at a time?
What can I do to achieve this effect? I want to animate the elements as they a开发者_运维知识库re added to Canvas one by one at a time. But the animation doesn't show up because the UI just freezes.
Thanks in advance:)
Perhaps you could use a timer, and add a smaller number of elements on each tick? That would let you ensure a few refreshes between each add.
On one project with lots of shapes on a canvas, I found it was helpful to pre-allocate and add all the shapes at the beginning, then just toggle their visibility. I don't know if that's generally useful or just specific to the situation I was in.
In VB.NET, we use Application.DoEvents() but I feel it is not possible in Silverlight.
Read this post: http://forums.silverlight.net/forums/p/12015/38635.aspx
Hope this helped!
Your code is being executed on the UI thread (the same thread that updates the screen). Hence, when you perform any computationally intensive operations your UI will freeze. Long running operations have to run in a separate thread in order for the application to remain responsive. Start a new thread and from within your thread iterate through your loop and instantiate your objects. To add the objects to the canvas you will have to use a Dispatcher
to ensure that part of code runs on the UI thread.
MyElement element = new MyElement();
Dispatcher.BeginInvoke(() => AddElementToCanvas(element));
精彩评论