UWP - How to save frame backstack and current page? - c#

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

Related

.NET MAUI: Switching the App's MainPage multiple times leads to exception

The root of a .NET MAUI application is the class App : Application.
This class has a property Page? MainPage that contains the current root page.
I try to use this property for navigation. I wrote a method App.ShowPage(Page page) that sets the property MainPage to the given page.
This works like a charm! I start my application and show a login page - works.
When the user clicks login on my login page I show the main view (a TabbedPage) - works.
When the user clicks on an item on that main view a details view gets shown - works.
Now when the user clicks cancel on that detail view, I try to navigate back to the main view. As always with my method App.ShowPage(Page page). Now I get an exception:
IllegalStateException; The specified child already has a parent. You
must call removeView() on the child's parent first.
So the error message tries to tell me what to do. And I do find properties like Element Parent or Element RealParent on the Page page I try to open - but they are all null. So how do I remove the page from that parent? What is the removeView() method in the .NET MAUI world?
Thanks in advance for any help!

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?

Pages appear to be cached, showing old data

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.

Saving page state in a 3 page app in WP7

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.

Navgation between XAML pages

I have a simple Silverlight application that consists of four pages (XAMLs).
Navigation is done by calling:
//from XamlPageA
this.Content = new XamlPageB();
Is this the right way. I need to have the entries in Browser history so that users can go page to the previous page(s). How can I do it.
You are bypassing the navigation system completely by setting content manually. You would have to implement updating the browser history yourself if you do it that way (certainly possible, but quite tedious).
A simpler approach is to generate a "Silverlight Business Application" project and see how the page navigation is simply handled with hyperlink buttons. All the browser history plumbing is done for you as is the mapping from URL to views.
e.g. A button with NavigateUri="/Home" will cause a view named Home.xaml to load into the navigation:Frame of the MainPage window.
if you look into the navigation:Frame element of MainPage.xaml, you will see a number of UriMapping entries like this:
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
They provide the pattern matching to convert from URLs to views.
Hope this helps your project.

Categories