I am confused what I should do in constructor and what else should I do in a onNavigatedTo method in a Windows Phone page.
Particularly, where should I read storage and settings?
onNavigatedTo is performed all the time when you navigate to this page, i.e. after navigation back from another page or deactivation application.
Page constructor is performed only once BEFORE page is loaded (after tombstoning it also needs to be loaded again)
Related
So I have a Shell that has a home page and a settings page.
The settings page makes changes to a singleton service that the home's ViewModel is binded to (MVVM).
I have set up break points on the PropertyChangedEvents of elements on the Home page and they hit as soon as changes are made to the service's properties and their PropertyChanged is executed.
However, upon going back to the home page via the Shell, the page's UI only updates when the page is active and visible. This causes fields to flash when they change. But the breakpoints suggest that they should have updated before.
Is there a way to force refresh the UI before or are there any other alternatives?
For now I have made my view perform a Fade In animation so that changes to the ViewModel don't cause flashing.
Even I have a Xamarin Forms Shell app, and I also had the same issue, So what you can do is, Make the common properties Global as in make those properties in App.xaml.cs and mark them as static.
So when the User changes the property on settings change the App.PropertyName.
Then on the OnApearing method of homepage call a method on the ViewModel say LoadDataValues() and read the App.PropertyName and modify the UI as needed.
What this will do is just before the HomePage appears it will keep the UI of HomePage ready.
Let me know if you have further issues.
I'm doing an application on windows phone
I use this method to navigate between pages
NavigationService.Navigate(new Uri("/SecondPage.xaml",UriKind.Relative));
But if I navigate between 2 complex pages (e.g many images on these), the system delay quite a lot of time(2-3s) for navigating
I want to navigate to next page before loading data in second page, when navigating completed then just start loading data for this page
it should be like MARKET PLACE app in PHONE Device, it's so fast
The page constructor and code in the Loaded event handler are executed before the first frame of the page is shown.
So if you want pages to be loaded fast, you should limit long-running operations like loading images in these methods.
One way that you can do this is to override OnNavigatedTo and start long-running operations in this method. OnNavigatedTo is called when a page becomes the active page.
I looking some information about lifecycle of pages, especially when the page constructor is called?
This happens when page start, even if it had been showed 5 sec earlier?
The Components of page are initialize every time when page shows and are destroyed when another page shows?
You can find some information about page lifecycle and events being called in this blog post
Page State - step by step
A page is instantiated when you navigate to it (assuming you are running on Silverlight). When you navigate away from it the state will be saved in a stack. If you navigate back (by calling the GoBack method on the NavigationService class) the page will be resumed from its state, without calling the constructor.
However, if you navigate away from an existing page, and re-navigate to the page by calling the Navigate method, a brand new instance of the page is instantiated, and thus the constructor will be called again.
So, to answer your question, the only sure-fire method that will be called when a page is shown is OnNavigatedTo (and OnNavigatedFrom when it's leaving, be it destroying on just navigating away). It is advisable to put initialization code and disposing code in the two methods, instead of relying on the constructor.
The loaded event on my MainPage is getting called twice on application startup.
The constructor of the page is only getting once, and I am pretty sure that I am only assigning the event handler once also. What is going on?
Took me a while to work out what was going on, but it turns out the loaded event shouldn't be used on pages for initialization tasks, as it is not guaranteed to be called only once.
OnNavigatedTo should always be used instead. See the MSDN article below.
Typically, you use the OnNavigatedTo method instead of creating an
event handler for the Loaded event. The OnNavigatedTo method is
preferable because it is only called once for each time the page
becomes active. The Silverlight framework raises the Loaded event each
time the element is added to the visual tree, which potentially can
happen more than once when activating a page.
I want to access a property of the main page of my application from inside the Application_Launching event. Is this safe to do? Does the page(-object) already exist at this point?
The application lifecycle model goes something like this:
App class constructor.
App.InitializePhoneApplication method (this is where the PhoneApplicationFrame that hosts page content is created).
App.Application_Launching.
Page constructor for the startup page.
App.CompleteInitializePhoneApplication (this is the first time outside of the Page class that you can access the page (from RootFrame.Content, though it's visual tree will not yet be created).
Page Loaded event (at this point the visual tree for the page has been loaded).
What is it that you are trying to achieve? You could set a value in isolated storage and read it in the page, or expose the value as a property in your App class.