WP7 Development Tip of the Day: Page Startup: Loaded event vs. OnNavigatedTo method
Cited from http://www.codebadger.com/blog/
In the last tip, we looked at one place you can execute “startup” code for your pages:the constructor. Today we’ll look at two other places you can consider for this code: The Loaded event and the OnNavigatedTo method. Spoiler: Use OnNavigatedTo.
OnNavigatedTo
The OnNavigatedTo method is called every time that one of your pages is, well, navigated to. Just what you would expect.
OnNavigatedTo is a method on the Page class, which is inherited by PhoneApplicationPage, which is then inherited by all of the pages in your Windows Phone 7 project. The method is a protected method, so all you need to do to use it in your page is override the base method.
1: protected override void OnNavigatedTo(NavigationEventArgs e)
2: {
3: base.OnNavigatedTo(e);
4: // Write your code here
5: }
If you create a new Windows Phone 7 List application, you’ll see that the Visual Studio template already has code written for the OnNavigatedTo methods for the two pages in the project, which is a pretty strong hint that the OnNavigatedTo method is a good place for “startup” code.
Common code for the OnNavigatedTo method includes setting up the datacontext, reading from the querystring, and setting up view-models. It’s much like the famous Page_Loaded event handler from ASP.NET WebForms. Which brings up the question – what about that Loaded event?
Loaded
The Loaded event is an event on the FrameworkElement class, so is a much more fundamental Silverlight concept than OnNavigatedTo, which is specific to pages. Lots of things have the Loaded event: borders, controls, images, panels, texblocks, shapes, etc. but only Pages have the OnNavigatedTo method.
Using the loaded event is a two-step process like any event: subscribing to the event and implementing a method.
1: public MainPage()
2: {
3: InitializeComponent();
4: Loaded += MainPage_Loaded;
5: }
6:
7: private void MainPage_Loaded(object sender, RoutedEventArgs e)
8: {
9: throw new NotImplementedException();
10: }
Which to Use
Both the OnNavigatedTo method and the Loaded event seem to fire any time I navigate to a page, both the initial time and using the back button. If someone knows of a time when one will fire without the other, please leave a comment and let me know. If you’re curious, the OnNavigatedTo method is called before the Loaded event fires.
As to which to user, the answer is OnNavigatedTo for a couple of related reasons: First, the OnNavigatedTo method is a page-specific thing, designed for pages instead of a more generic event like Loaded. Second, this warning from the Silverlight 4 MSDN article for the OnNavigatedTomethod indicates that it’s best not to use the Loaded event for page startup tasks:
Typically, you use the OnNavigatedTo method instead of creating an event handler for the Loaded event. The OnNavigatedTo method is preferable because it is only called once for each time the page becomes active. The Silverlight framework raises the Loaded event each time the element is added to the visual tree, which potentially can happen more than once when activating a page.
The same warning does not appear on the WindowsPhone documentation for the OnNavigatedTo method, but I would guess a similar warning applies.