check if data is already loaded in windows phone app - windows-phone-7

I am coding for a basic windows phone app. First time when the application is getting launched, data is getting loaded properly in listbox. Now after clicking on any item in list box, I am navigating to another page. But when I press back button to come back to main page. a new set of similar data is getting appended to list box items. how do I check whether data was already loaded or not.
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
In my call back MainPage_Loaded I am binding my data.

You should see how navigation works in Windows Phone. When you go back, the Loaded event gets raised every tmie.
The constructor, however, gets called only when the page is created the first time. When you go back, you actually don't call the constructor again, the page is cached. So, in order to stop the data from loading again, you might want to try loading it in the constructor. That's the simplest way.
The other way includes some form of a bool flag which keeps track of that.

Related

How do I get the Router navigate extra "replaceUrl" to work correctly?

I have a contact addition form that can be navigated to from multiple screens in our application.
Once the form is submitted, I then take the user to a screen to view the contact that was added.
When the user then makes use of the back button it should take them back to the screen that they originated from.
This might be the Android back button or one that calls the RouterExtensions back function.
I have made use of the navigate extra replaceUrl when navigating away from the form to the view page.
I have also tried using the skipLocationChange extra when navigating to the form but this creates more issues.
I have created a simple playground page flow that creates not quite the same issue but does throw an error that I don't know what to do with either:
https://play.nativescript.org/?template=play-ng&id=BfVcGZ&v=2
In our app, by making use of the replaceUrl extra, the back button does take the user to the correct page.
However, there is a brief moment where they see the form again. This isn't an ideal user experience.
In the linked Playground I do get an error:
Cannot reattach ActivatedRouteSnapshot created from a different route.
This seems to tell me that replaceUrl is indeed removing the page from the route table.
However, the page isn't destroyed yet and so the app is trying to show a page that it shouldn't.
replaceUrl is not yet supported by Page Router Outlet, there is an open feature request, you might want to register your vote on the feature and follow up there for further updates.

NativeScript: how to save and restore a page state?

When we navigate to a page it reloads every time. If I previously created some views dynamically in it either programatically or using ui/builder all of this will be lost. Is there a way to save / cache the state of the page and restore it after the page reloading?
Set a boolean variable when you have loaded a page. Whenever navigating back to that page, check the status of the boolean variable in the pageLoad/onNavigatedTo hooked up functions before the actual code in that page.
I'm afraid of that there is no built-in feature for that.
What you can do is to store/reload manually your screen's data in application-settings at the unloaded and navigatingTo events.

MVC 3. Load menu and content separately

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.

Questions regarding backkey press of Wp7 Phone Application

Question regarding state of pages while deactivation and activation of page,i save the datas for that particular page alone.when i go back to previous page,by pressing back button,I am getting previous page which does not have data in it.To load the data in the previous page i need to get the instance of previous page ,is there anyway to get the instance of previous pages in Phone Application stack.,Other thing,is that On backkey Press,handling onbackkey press,by navigating to the new instance of the previous phone application page(what will happen to old instance of the previous pages).how to handle these scenarios?
You have asked some questions recently on page state. When an app is deactivated and activated again Windows phone 7.5 saves the page state. All that you need to do is to carefully handle the Page_Loaded and OnNavigatedTo events upon activation.
And if you want to override the back stack OnBackKeyPress, you can do so and navigate to any other page instance. But you should ensure you removed the back stack entry. otherwise causes many problems.(but this is not recommended)
NavigationService.RemoveBackEntry();

New Instance of the page on Navigation

I have a few pages in an Application that require A-Synchronous calls to be made for about 2-3 minutes to get Synchronized, the user may navigate away from that page during Synchronization and can come back again after visiting multiple pages and the sync continues all the time he is on other pages as well, when I go to a page from sync-page and press the Back button everything works fine.. but when i go to a page and navigate back to sync-page from Application Bar a new Instance of the Page is created and the Sync is just like Re-started.
Now i know every thing is working fine since new instance of a page is created when i call NavigationService.Navigate() , but what should i do in this scenario ? How to get the old instance of a page if it is there ?
Thanks...
You can't get an "old" instance of a page and it's not guaranteed that a backwards navigation will reload the previous instance of the page, it may be a new instance of the same page, but restored to the same state (assuming you saved any).
If you are trying to provide backwards navigation from the application bar then a) you probably shouldn't because that's what the back button is for, and b) you should make sure you use NavigationService.GoBack() instead of NavigationService.Navigate() because Navigate will always launch a new instance of your page.
If the page you want to get to is not the previous page, then it sounds like you are trying to implement non-linear navigation for which there is a recipe on the App Hub.
By the sounds of your scenario, you should handle this long running process separately (away from the view) and then display it's progress or results in a view when the user navigates to the relevant page.

Resources