I have two pages (say page1 and page2) in my app. Page1 has two lists - a list of completed tasks and a list of incomplete tasks. Page2 shows the task selected and gives an option to mark the status as complete or incomplete. When you mark as complete, it creates a new page1 and navigates to that - and this new page1 should now show that task in the 'complete' list. Unfortunately it doesn't, it's still in the 'incomplete list'. If I close the app and open it again, the task will be in the 'complete' list like it should be. It's as if a cached page is displaying, but a new one is being created on navigation. Anyone have any ideas why this is happening?
Why are you navigating to page 1? you should just do NavigationService.GoBack(). Also, make sure your view models properly implement INotifyPropertyChanged ... that way, when you change the property, any view elements that are watching it will automatically update themselves through the binding mechanism.
Related
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?
Hi I have this hierarchy
App (RootFrame)
Login Page
Main Shell (InnerFrame)
List Page with entities 1
List Page with entities 2
Detail Page for entity 1 (InnerFrame2)
subpage 1-1
subpage 1-2
Detail Page for entity 2 (InnerFrame3)
subpage 2-1
subpage 2-1
And I want to handle Back button correctly. Navigating between pages in inner frames is ok, but when I go for example from "List Page with entities 2" to "Detail page for entity 2" and then back, MainShell i recreated and forgets the InnerFrames BackStack and opened Page(should be "List Page with entities 2")
Is there any elegant way to handle this? for example save InnerFrames history and current page when navigatedFrom at MainShell happens?
You can try Prism Framework, here is a lifecycle managing example.
In a nutshell Prism provides SessionStateService to manage view restoring when OnAppSuspended and OnAppResumed events invoked.
Also Prism NavigationService passes NavigatedToEventArgs e, NavigatedFromEventArgs e, Dictionary<string, object> viewModelState parameters and your page behaviour can be overrided in OnNavigatedTo and OnNavigatedFrom using this parameters.
I found what I needed. There are SetNavigationState(string) and GetNavigationState() methods on Frame object. Thanks anyway
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.
Well it's my first question here and as far as i searched i didn't find a solution for my problem. I am coding my first rssreader app for WP7 and i am facing a problem with the page state. I have 3 pages and navigation goes like that
mainpage >>> listitemspage >>> detailspage. Mainpage is a databound model with preconfigured feeds category items (urls and names), listitempage is a page in which i implemented a webclient to read feed items and detailspage is where i pass the details of the selected feed item from listitempage. Page state working in these situations:
mainpage >>> start button >>> back button (OK)<br/>
listitempage >>> start button >>> back button (Doesn't work)<br/>
detailspage >>> start button >>> back button (OK)<br/>
In detailspage i save the page state using OnNavigatedTo/From and State.TryGetValue
This seems to work but if i create a button with a browser task so the user can navigate to the full article when the browser open and user press back button the details page state works. If the user press for second time the back button then the app is exiting instead of navigating back to the listitempage. So i have this problem too:
detailspage >>> browserbuttontask >>> backbutton >>> restorepage >>> backbutton->exit<br/>
Any ideas?
I'd recommend using the Tombstone helper on codeplex written by Matt Lacey (of the Windows Phone User Group).
http://tombstonehelper.codeplex.com/
It will either solve all your problems very easily or at the very least show you how to write your page states off to isolated storage for long term use (remember the state cache only survives while the device is powered and so long as the phone doesn't need to clear it's memory)
Hope this helps
Rgds
Simon
If you want to save page state without messing with IsolatedStorage try SterlingDB. It uses IsolatedStorage but it is very simple to setup and use. You can add sterling via nuget. You will have to stick data you want persisted in a class but saving and loading is simple.
Beyond that you also might want to look at Caliburn Micro. It adds page lifecycle to your ViewModels like OnInitialize, OnActivate, OnDeactivate. It also adds some nice stuff for WP7 like automagically persisting properties on a ViewModel to IsolatedStorage or phone state.
http://www.sterlingdatabase.com/
It sounds like your application is throwing an exception when restoring state to your list page, which has the effect of terminating the application.
Have you tried doing all this with the debugger attached? It should show you what exception is being thrown and where.
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