OnNavigatedTo vs Load event
Solution 1
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.
Solution 2
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.
Solution 3
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.
balexandre
Father (x3), Husband (x1), Dedicated, Friendly. Web, Windows and Mobile developer (mostly .NET and NodeJs). Photographer by hobby, Windsurfer by passion. Born in Portugal (in 1977),happily married to a beautiful Romanian gal (since 2005) and living in Denmark (since 2006) 😊 P.S. My Simpson character was designed by a Fiverr.com user.
Updated on June 26, 2022Comments
-
balexandre almost 2 years
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 likepublic 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 theLoad
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?-
brendan over 10 yearsIf you are doing a lot of initialization, then you will want to use Loaded as opposed to OnNavigatedTo or else your page may take some time to load.
-
-
jazzy almost 10 yearsHmmm...should Tigran's answer have been accepted then? I wonder if this is still true in 8.1
-
jazzy almost 10 yearsA few of the MSFT examples (e.g. msdn.microsoft.com/library/windows/apps/ff967547(v=vs.105).aspx) put their code in OnNavigatedTo and use a bool in the page constructor to determine if it needs to be executed.