For example, I create a Canvas that contains a big amount of Shapes like this:
var canvas = CreateCanvasThatContainsShapes();
Then I add all the canvas to main page:
layoutRoot.Content = canvas;
It takes not so much time for those two lines of code to run, but it take a while for all the shapes to show up on the screen, and the UI will be unresponsive for a while.
In WPF, I can test the visual tree rendering time by doing something like this:
//create logic tree and add it to main page
stopWatch.Start();
Dispatcher.BeginInvoke(new Action(()=>{
stopWatch.Stop();
//Show the ellipsed time
}),DispatcherPriority.Loaded);
This way, the Stopwatch starts after the logic tree is 开发者_StackOverflowcreated and stops after the visual tree is rendered(loaded).
But in silverlight, there is no DispatcherPriority, then how can I do the similar thing?
Thanks
There is an event that you can use to help determine the time taken to render to screen; LayoutUpdated. This event is fired whenever a layout parse completes. Start your timer before you set your layoutRoot.Content
, and stop it when the event fires. This tells you how long the render process took.
Another useful event to look out for is CompositionTarget.Rendering, this event fires each time the UI is rendered. You should find that this event fires very often, however when you add your complex visual tree, you will see a gap in the stream of events.
精彩评论