balexandre balexandre - 25 days ago 21
C# Question

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?

Answer

Reading from documentation about OnNavigatedTo:

Called when a page becomes the active page in a frame.

and when we read about Loaded event see:

Occurs when a FrameworkElement has been constructed and added to the object tree.

They are completely different, as page, correct me if I'm wrong, can become active more then one time during the lifetime of your application, but constuction of the FrameworkElement usually happens once.

Comments