In several online examples I found this:
public partial class ForecastPage : PhoneApplicationPage
{
Forecast forecast;
public ForecastPage()
{
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.
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.
In Windows Runtime, the Loaded event will always fire after OnNavigatedTo (even when pages are being cached by setting NavigationCacheMode.Required). Vitalii is right about that.
According to MSDN:
In the Windows Runtime implementation, the Loaded event is guaranteed to occur after a control template is applied, and you can obtain references to objects that are created by applying the XAML template.
For app code that uses navigation between pages, do not use Page.OnNavigatedTo for element manipulation or state change of controls on the destination page. The OnNavigatedTo virtual method is invoked before the template is loaded, thus elements from templates aren't available yet. Instead, attach a Loaded event handler at the root of the newly loaded page's content, and perform any element manipulations, state changes, event wiring and so on in the Loaded event handler.
But there is a good reason why you would want to use OnNavigatedTo: it is the only place where you can get the navigation parameters. If you never use navigation parameters, use the Loaded event.