Search code examples
c#initializationwindows-phone-7

OnNavigatedTo vs Load event


In several online examples I found this:

public partial class ForecastPage : PhoneApplicationPage
{
    Forecast forecast;

    public ForecastPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // code here
    }
}

but in others I found the use of the Load event like

public partial class Person : PhoneApplicationPage
{
  private PersonViewModel _ViewModel;

  public Person()
  {
     InitializeComponent();
     this.Loaded += new RoutedEventHandler(SearchView_Loaded);
  }

  void SearchView_Loaded(object sender, RoutedEventArgs e)
  {
     // code here
  }
}

I know that OnNavigatedTo fires before the Load event, but both fire before the UI is drawn into the phone, so my question is Is there any advantage in use one method from the other?


Solution

  • I'd disagree with Tigran.

    public View()
    {
      InitializeComponent();
    
      personList.ItemsSource = PersonDataSource.CreateList(100);
    
        Loaded += (sender, args) => Debug.WriteLine("Loaded");
    }
    
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
          Debug.WriteLine("Navigated");
      }
    

    While jumping forward-backward, output is

    Navigated Loaded Navigated Loaded Navigated Loaded

    So, OnNavigated is called when page navigation is done, but before(during) page controls are loaded, while Loaded is called when page is ready and all controls are loaded.