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.
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 have an app with 3 sections:
Main menu;
Context Menu - Related to selected item in main menu;
and Page body - Related to selected item in context menu;
"Main menu" and "Context menu" are based on membership. I don't want to load them everytime my page loads, because that would consume resources database. So, I'm using ajax to load main menu only one time, and when an item is selected, I load the context menu for that item.
My problem is: Every form's post will erase my menu.
Question: Will I have to build my entire application using ajax? I don't wanna do that, because it is too much simpler do a post in the form then send all data to controller with ajax.
Until now, I have 2 options:
Load my menus with ajax and the page body with IFRAME, so the post's will not render again my menus.
Do everything using ajax;
Is there any alternative to load my menus with ajax and be able to use form's post?
Sorry if I wasn't clear enough.
The sentence that gave me a pause is this "I don't want to load them everytime my page loads, because that would consume resources database."
You see, I've build quite a lot of apps, that display menus and sub-menus based on user roles (what you called membership). This has never been an issue from the resources or database perspective.
You can access all the membership information that you need once, when your used is being logged in. In the simplest case user's identity will be stored in the context along with the roles they have (HttpContext.User), so you do not to need a database lookup at all to get this information on every request. Note that with this scenario no ajax is required either.
If for whatever reason you can't store your membership information in the context like this, you still can store in in session (if in-memory) or in encrypted Cookies.
Now, I understand, that I don't all the details of your scenario, and that may be in your scenario what you are trying to do is warranted, however I suggest you think it through again, as under normal circumstances what you indicate is a problem (database resource) should not be a problem at all.
The bottom line is: if you alter your application that it stores the membership information when user logs on you won't have your problem to start with.
You don’t have to build all of your application using Ajax. But in this scenario Ajax may be the best way forward.
Following is my suggestion
Create your data entry for inside a dev
Have each input controller marked with a class (say ‘dataEntry’)
Create a javascript function to iterate the dev and build a list of all elements that has class dataEntry
Build a json object using the list. Use the name of each element as property name and value as the property value
Use jquery ajax to post this to the controller action
[optional] you can use .done and .fail methods to take action on success or failures of the call
I know this may look intimidating, but if you have many data entry forms, you can re-use this code.
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 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)