Windows Phone persistent instance of page through app life-time - c#

I am working on a simple Windows Phone app, which has basically those functionality:
Page 1, there is a button to get user's location, and navigate to page 2
Page 2, use the user location and map control to display a map and view center is user's location. There is a app bar button to navigate to Page 3.
Page 3, Show some nearby place list in this page, and once user select one of the places, it will go back to page 2 to Pin the place on Map Control.
So here is the question: I want to keep page 2 data alive in this case so I dont have to re-initialize page 2 everytime I go back from page 3. It would be perfect I can keep an instance of page 2 somewhere, and use that everytime I want to come back from page 3. So far I know there are two approach, one is in Page2 xaml I can have something like:
<Page
x:class "Page2.keepPageAlive"
...
The other one is using AddBackEntry. But I am not sure which one is fit in my case, or is there any other good approach.
Please share your idea, thank you.

As long as you navigate to page3 from page2, and go back to page2 using NavigationService.GoBack(), the page2 instance will be kept alive. So you basically have nothing to change.

In the WP7 Navigation stack, Page 2 will still exist. So if you navigated from Page 1 to Page 2 to Page 3, all 3 pages exist.
If you call NavigationService.GoBack() from Page 3, it will pop Page 3 off and restore the previous instance of Page 2, not create a new instance of Page 2. This is also what happens when you hit the Back button.
Only if you call NavigationService.Navigate() would a new instance be created.

Related

Is it possible to dynamically load an .aspx page?

I received a new requirement today: For our product page, they want a completely different layout to be used based on the product type.
For example, say we sell buckets. Currently, all buckets use the exact same page layout. But now, they want wooden buckets to use the current layout, and plastic buckets to use a completely different layout. However, they want the URLs to stay the same (e.g., domain.com/bucket/1), so I can't just forward plastic buckets to a new page.
The current page structure is as follows:
CurrentMasterPage.master > CurrentProductPage.aspx > Several UserControls
The new layout requires new pages (i.e., none of the current ones are reused):
NewMasterPage.master > NewProductPage.aspx > Several UserControls
My first thought was to take all of the markup and code from CurrentProductPage.aspx and put it into a UserControl, then create a second UserControl for the new layout (NewProductPage.aspx), and have CurrentProductPage.aspx dynamically load the appropriate UserControl based on the product type, however, this doesn't work because the new layout requires a new MasterPage, and I can't reference a MasterPage from a UserControl.
Then, I thought about using URL Rewriting, but I don't think it's possible to have the same URL load two different pages.
Is there any way to accomplish this?
Why not use a 100% server side re-direct?
When you use response.Redirect("some different page"). Then the client side browser is sent a whole new copy of that page, and the URL will be updated.
However, the server side can write any page it wants to. The client side will not even know the server decided to dish out a different page for the given URL.
So, you could have a page with fake tabs as buttons. When the user hits a button, the browser round trip starts (for the given URL). But on server side, you can then dish out a different page for that URL.
So, in place of this classic "round trip", you can use:
Server.TransferRequest("MyotherWebPage")
So, for the given URL, before the current page (based on given URL) is sent down back to the browser, the above will simply pump out a different page. The current page will never make it back down to the browser.
In fact for a rich page with lots of buttons and features, you can change the page displayed. So in on-load - simply in place of a "response.Redirect", use a server.Transfer. The current page never makes it to the client - the one you dish out where. Because the client side has zero clue about what the web server decides to dish out - it will also have zero clue that a different page was send back to the client.
Try the above with a test page.
On page A, behind a standard button, jump to web page B
eg:
Response.Recdirect("MyPageB.aspx")
Note the URL change - classic round trip.
Now, do this with the button:
Server.Redirect("MyPageB.aspx")
In this case, no full round trip occurs. The server transfers directly to the new page and sends that out. (and note how your URL does NOT change).
You can change the Master Page on PreInit on the Page using a Master. This is possible because a Master is basically the same as a User Control and is loaded AFTER the page's code behind.
protected void Page_PreInit(object sender, EventArgs e)
{
if (NewProductPage)
{
MasterPageFile = "~/NewMasterPage.master";
}
}

Open new page outside masterdetailpage

I just new to Xamarin.Form with Prism. I want to load the page in different behavior. The first one I achieve already is in image below.
But I want to do a behavior like below image. Load a new page outside master detail page. How can I do it in prism?
You have figured out how to display the "Hamburger" icon as well as title by doing the following.
NavigationService.NavigateAsync("MasterPage/NavigationPage/DetailsPage")
If a user makes a selection from the actions listed on the master page. For Example, let's say settings.
You have a couple of options here, you can do navigate relatively
NavigationService.NavigateAsync("Settings")
This will navigate to the settings page. This will also display the back button as the second image. Your current page path will be
MasterPage/NavigationPage/DetailsPage/Settings
Now let's say you want the settings page to be the top details page. You have to navigate to it via an absolute path.
MasterPage/NavigationPage/Settings
NavigationService.NavigateAsync("MasterPage/NavigationPage/Settings")
You need to add a new page to the stack, page over existing one. To do that you should do navigation like this:
navigationService.NavigateAsync("Settings")

Navigate back without removing actual page from BackStack

I'm developing a WP 8 app and I've a problem with navigation.
I can't find a way to navigate to a page on my back stack without losing my actual page state.
The situation is this:
I'm on page A and navigate to page B, so the back stack only contains A.
On page B I want to navigate back to page A but with page B being persisted on the back stack, so when I'm on page A again I can go back to page B preserving it state.
So this is it: A --> B --> A --> B, it's kind of a cycle but it isn't one. Page A shows items that are also shown in page B with less detail, but from page B I can go to page A to see the details and then back and back again.
It may seem complicated but I assure you its pretty intuitive :) it works like this on Android and iOS.
Is there anyway to add page B to the navigation stack before page A so when I call NavigationService.GoBack() it goes back to page A with it previous state and then do the same from A to B.
Thanks in advance!!!
There are a couple of options. If you use the standard MVVM pattern then you're not really saving "page state" (unless you mean things like scroll position in lists or selected text control) - you're saving data. And that data will be there the next time you navigate to Page B. You can watch this video and the MSDN article it points to for more information on databinding and how to store the data in global state that will be re-bound to the page the next time you navigate.
If databinding doesn't solve the problem, another option is to not use different pages at all, but to just have A and B as two different containers on the same physical page. You can then trap the back button to fake going "back" when you need to toggle back to Container A.
Use this help topic to learn how to preserve and restore page state for Windows Phone 8. Navigation stack can store only page URI and a small amount of data (in the URI parameter).
Like this:
NavigationService.Navigate(new Uri("/SecondPage.xaml?var=value", UriKind.Relative));
At the SecondPage.xaml you can get the value of val. Here is detailed example.
if (NavigationContext.QueryString.TryGetValue("val", out msg))

Windows Phone 8 Databound App and back button

I've something that I can't get myself understand. I'm making an app with databound template. I put a textbox on mainpage and a button. when i type something in textbox and press on button it navigates to the listing page and that content comes from web and then if i press on back button and make a new search the results from previous search stays there. how can i reset/clear or disable cache of that page?
It would be helpful if you could post your XAML and code-behind, but I will attempt to make a jab at an answer. Where are you referencing the call to get the data from the web? If it is in the constructor of the page, then that is why the previous search stays there. What is probably happening is the first search constructs the secondary page, does your web call, and binds your data to the page. Then when you press the back button, and click it again, the page is already constructed, so it uses the same data.
It is probably wise to call your web service in the OnNavigatedTo override method. From the first page, you can pass parameters to your secondary page (i.e., pass the search term, then pass the search term into your web service).
Here is an example of passing parameters between pages: http://developer.nokia.com/Community/Wiki/Passing_parameters_while_navigating_between_pages_on_Windows_Phone
Also, make sure your Data Context for the second page is appropriately set each time the page is navigated to, since you have a data bound application.
Without code, I can't really help other than giving these things to think about.

Windows Phone Silverlight page navigation

I have two questions here about Windows Phone page navigation:
Is there a way to get the instance of the page I am navigating to? That is, if I am on page one and want to navigate to page2 to when button click, can I get the page2 instance after page2 is initialized by NavigationService.Navigate("page_2_uri") call in page 1?
Is there a way I can know which page I navigate from? For example, I am currently on page3, and I want to do something like: if page 3 is navigated from page 2, I will do this, otherwise I will do that.
Thank you.
Is there a way to get the instance of the page I am navigating to?
No.
Is there a way I can know which page I navigate from?
Yes. Traverse the NavigationService.BackStack
The idea for using the NavigationService to navigate between pages is that you don't need to know any details about your destination. So in your example, Page 2 isn't initialized until you've left Page 1, and therefore Page 1 is no longer in scope, and won't be able to do anything with Page 2. If you want to pass information/context to Page 2, id recommend using Query Parameters (see next answer). If you want to know where the navigation is going, you can override the OnNavigatedFrom event and look at the Uri property of the NavigationEventArgs.
I would recommend looking at the NavigationContext property of the Silverlight Page class. This property lets you view the QueryString of the navigation request. Using this approach, you could navigate to page 3 using a uri like "page_3?previous_page=2" and then extract the previous_page from the QueryString of the NavigationContext to see where you came from.

Categories

Resources