WP7 Development Tip of the Day: Page Startup: Loaded event vs. OnNavigatedTo method

by brad 5. October 2010 15:42

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 OnNavigatedTo method 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.

 

Other Resources

More Tips

Tags:

Windows Phone | WP7 Tip of the Day

Comments

10/5/2010 4:25:19 PM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        WP7 Development Tip of the Day: Page Startup: Loaded event vs. OnNavigatedTo method
        [codebadger.com]
        on Topsy.com

topsy.com

10/27/2010 6:01:28 AM #

trackback

Дайджест технических материалов #5 (Windows Phone 7)

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

Oleksandr Krakovetskiy blog

10/27/2010 3:54:39 PM #

trackback

Archived Tip of the Day by Category

Archived Tip of the Day by Category

code badger

5/17/2012 10:12:06 PM #

pingback

Pingback from dancecoder.com

Windows Phone 7 资源汇总 | DanceCoder

dancecoder.com

Comments are closed

About Brad

Brad Tutterow lives in Illinois and works in Missouri. He has 12 years of experience developing web sites and Windows applications using a variety of technologies and is most excited currently about Silverlight, Windows Phone 7, Halo Reach, and Visual Studio 2010.