Very first let us understand what I mean by word "Context Switching" here? Context switching in context of this post is essentially, navigate back to the page from where the application got deactivated. There are three steps we need to do to perform above task
Define isolated storage setting globally in app.xaml public IsolatedStorageSettings isoStorage = IsolatedStorageSettings.ApplicationSettings; Find URL of current page and store in application deactivated event You can find URL of current page in the deactivated event as below. Read it and store it in isolated storage. private void Application_Deactivated(object sender, DeactivatedEventArgs e) { Uri currentUri = ((App)Application.Current).RootFrame.CurrentSource; isoStorage["data"] = currentUri.OriginalString; } Read and set the start page in application activated event Next step you need to do is on the application activated event
private void Application_Activated(object sender, ActivatedEventArgs e) { string CurrentURI = (string)isoStorage["data"]; if (CurrentURI != "") { Uri uritoNavigate = new Uri(CurrentURI, UriKind.Relative); ((App)Application.Current).RootFrame.Navigate(uritoNavigate); } else { Uri nUri = new Uri("/MainPage.xaml", UriKind.Relative); ((App)Application.Current).RootFrame.Navigate(nUri); } } You need to perform same steps in application launching event also as of application activated event. private void Application_Launching(object sender, LaunchingEventArgs e) { if (IsolatedStorageSettings.ApplicationSettings.Contains("data") == false) { isoStorage.Add("data", "/MainPage.xaml"); } string CurrentURI = (string)isoStorage["data"]; if (CurrentURI != "") { Uri nUri = new Uri(CurrentURI, UriKind.Relative); ((App)Application.Current).RootFrame.Navigate(nUri); } else { Uri nUri = new Uri("/MainPage.xaml", UriKind.Relative); ((App)Application.Current).RootFrame.Navigate(nUri); } } Remove isolated storage value in application closing event private void Application_Closing(object sender, ClosingEventArgs e) { isoStorage["data"] = ""; isoStorage.Remove("Data"); } This is all you need to add in App.Xaml.cs file to perform context switching in your application.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: