WinRT - Navigate to specific page in the Navigation Stack - c#

I have these pages on a windows project
page1.xaml
page2.xaml
page3.xaml
page4.xaml
After i do some operations on page1 i call this;
Frame.Navigate(typeof(page2), document);
On page 2 i do some stuff with the document, then i navigate to page3.xaml
Frame.Navigate(typeof(page3), document);
Then i do some more stuff on page 3 and go to page4.xaml.
And in page4.xaml i have a button that when pressed i want to go back to page2,xaml not a new one, but the same one i came from.
is this possible?

To navigate to the same instance of page2, you need to set the Page's NavigationCacheMode Property to Enabled or Required :
NavigationCacheMode="Enabled"
now the difference between those two is whether or not they take CacheSize into consideration or not,
from msdn :
The difference in behavior is that Enabled might not be cached if the frame's cache size limit (CacheSize) is exceeded, whereas Required always generates an entry no matter the size limit.

Related

Data inside a LIst<String[]> dissappears (UWP)

the situation is the following:
On Page one i have a button that leads to page two and transports itself as an object to that site.
On page 2 i can set a few things that are stored in a List<string[]> that list is now moved by calling a method on Page one that sets the List on that page. and then i return to page one.
Everything is set and the data was received.
Now as soon as i try to access that data it gets null. I have no statement anywhere in the code that could set it to null. I only have to references:
1.
public void setMaterialData(List<string[]> data )
{
MaterialData = data;
}
and 2.
dataObject.materialdata = MaterialData;
and i already checked if the constructor is called again and overwrites it, but it doesn't. And strangely that happens with nothing else in my code so far.
Things that might be important:
I have the cache mode set to enabled,
I noticed that the constructor of the Window is called when i navigate away from page one to page two and i don't know why?(there is no reference to my list in there though)
Thank you in advance for your answer.
if you are not caching page then the newly navigated page is a new instance of the page and that maybe one possibility of getting field and property to Null
another possibility is resetting the variable as the page loads because every time the
page comes up to display it fires up page_loaded event if you are using that for initializing variable and other components than that may reset them to null
can you can check and confirm that if the navigated page is the same as the saved instance simply by checking (savedObj == this)

Previous page instance not maintained after navigation in prism

I am using the latest version of Prism.MVVM in Xamarin.Forms. In this, if I try to navigate to second page from the first page, the first page is initialized once again. i.e., the constructor of the first page is called once again.
For example, I am having Page1.xaml and Page2.xaml pages with their respective view models(those will be created and registered automatically while creating in prism).
I navigating to Page2 from Page1 like below,
NavigationAsync("Navigation/Page1/Page2")
While navigating, Page1.xaml's constructor is called so that the page is created newly which lead I could not able to maintain the Page1.xaml instance. Also, please note that Page1.xaml is a Master-Details page.
Is this a behavior in Prism? If so how can I overcome this?
Thanks in advance.
Navigating away from a XAML page destroys it in UWP. You can preserve a page’s state data (and avoid re-construction) by adding a single line in a XAML page’s tag:
NavigationCacheMode="Required"
Does it work the same in Xamarin?

navigating to same page in windows phone 8

In my WP8 app, i have situation where i have to navigate from one page to another and afterwards i need to reload the same page for some reasons.
MainPage.xaml --> Page1.xaml --> Page1.xaml --> Page1.xaml
When user press the backkey should go back to "MainPage.xaml" page.
I tried using the NavigationService.navigate() for navigation, some reason i couldn't able to reload the page. If i pass any unique query strings (eg: Guid) with navigation url, i am able to reload the page. But, when i press back button - it never goes back to Mainpage.xaml page.
Is there any best way to achieve this?
Pass in a query string every time you reload the page (such as your random GUID). On your OnNavigatedTo method check if the GUID query string exists. If it does exist, you know that you don't want this page on the Navigation Stack because it's the reloaded version, so you can remove it by calling NavigationService.RemoveBackEntry.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string guid = string.Empty;
if (NavigationContext.QueryString.TryGetValue("guid", out guid))
{
//guid exists therefore it's a reload, so delete the last entry
//from the navigation stack
if(NavigationService.CanGoBack)
NavigationService.RemoveBackEntry();
}
}
Use NavigationService.RemoveBackEntry method to remove last navigation stack entry.
You can also remove all elements from navigation history:
while(service.CanGoBack)
service.RemoveBackEntry();
and then add the one you're interested in.
NavigationService.Navigate(new Uri("/MainPage.xaml?" + DateTime.Now.Ticks, UriKind.Relative));
You can use it under a control to navigate to same page
Because I couldn't find any Windows 10 (Universal apps UWP UAP) questions about this, and this one seems to be top result on google search to reload a page, here is a solution:
NavigationService.Refresh();
I'm using Template 10 on my app, so I don't know if it matters here. The lib encapsulates NavigationService on its own INavigationService

QueryString for redirection!

I have used a query string parameter to redirect from Page 1 to Page 2. From Page 2, i wanted to redirect it to Page 3 if Page 2 has been called from Page 1. What should be the condition to check if the querystring is used??
Please help me out!! Thanks guys!
Ram, this is my interpretation of what you wanted: if user accesses Page2 due to redirect from Page1, the user will be redirected automatically to Page3. If user accesses Page2 directly (without visiting Page1), the user will stay in Page2 (no redirect to Page3).
If this is the case, inside Page1, you can set redirect("Page2.aspx?previouspage=page1"), then in Page2, inside Page_load method, check for the querystring, if previouspage exists and equals "page1", redirect to Page3, else do nothing and Page2 will show up.
You may wish to parse the HTTP_REFERER and see if Page 2 has been called from Page 1. Basically, you would build your Page 2 redirection based on the HTTP_REFERER value. In C#, the value is available using the following:
Page.Request.ServerVariables("HTTP_REFERER")
Put something into the query string of page 2 as well, and check that.
However, you should always move to reduce the number or redirects (some browsers limit at 5 - to prevent the case where while not an exact loop, the browser will redirect for ever - if it already took a couple of redirects to get to your site, then part way through this sequence, it'll stop redirecting). In Page 1 you should redirect to Page 3. If you need something done then do it in Page 1 or Page 3, but note that this is only from the browsers perspective; if you do a server.transfer to page 2 then while the logical pattern for your code is 1 -> 2 -> 3, to the browser it looks like 1 -> 3. In this case you can also check the raw URI if you wanted to know that you had got there by coming from page 1.
Looked at from another way, a redirect is part of your UI. If the user was on page 1 to being with this should have made sense, or there's a bug in your UI. If the user ends up on page 3, then this should also have made sense, or there's a bug in your UI. Now considering this, what is the purpose from the user prespective of page 2? I'm not saying there couldn't be one (I can think of cases where multiple redirects make good sense, esp. if they are of different types, e.g. a moved-permanently followed by a see-other makes sense), but chances are it doesn't and a you could deal with part of it while keeping the flow in the server.
Maybe I am not understanding this right .. but why not redirect directly from page 1 to page 3?

ScriptManager.AddHistoryPoint - page is displaying original page state first, then saved state

I'm using an UpdatePanel and the ScriptManager's AddHistoryPoint method to save my page state in the browser's history. The page is a simple search/results page with 2 states:
STATE 1 - showing search filters
STATE 2 - showing search results
When I navigate back to the page (by clicking the back button in my browser), the page shows the initial state (STATE 1) but then the update panel posts back and the page flips to the search results (STATE 2).
This looks untidy and it feels like I'm missing something here in my implementation. How can I tell the page to either just load the saved state, or hide the page content until the saved state has been loaded?
as I know in ScriptManager event OnNavigateHistory you must handle there that example you add HistoryPoint for state one and two same key ID only the value is different and thene OnNavigateHistory base on e.State["YourKey"] you make decision wich state to stay.
example
string indexString = e.State["YourKey"];
if (String.IsNullOrEmpty(indexString)) {
SetToStateOne();
}
else {
int index = int.Parse(indexString);
SetToStateTwo();
}
Sorry for my bad english

Categories