Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 374
balexandre Avatar asked Feb 22 '12 21:02

balexandre


1 Answers

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.

like image 190
Vitalii Vasylenko Avatar answered Oct 12 '22 15:10

Vitalii Vasylenko