// Code to execute when the application is activated (brought to foreground) // This code will not execute when the application is first launched private void Application_Activated(object sender, ActivatedEventArgs e) { if (PhoneApplicationService.Current.State.ContainsKey(ModelKey)) { ViewModel = (MainViewModel)PhoneApplicationService.Current.State[ModelKey]; RootFrame.DataContext = ViewModel; } }
// Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private void Application_Launching(object sender, LaunchingEventArgs e) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = new IsolatedStorageFileStream("data.txt", FileMode.OpenOrCreate, FileAccess.Read, store)) using (var reader = new StreamReader(stream)) { if (!reader.EndOfStream) { var serializer = new Newtonsoft.Json.JsonSerializer(); ViewModel = (MainViewModel)serializer.Deserialize(reader, typeof(MainViewModel)); } } // if the view model is not loaded, create a new one if (ViewModel == null) { ViewModel = new MainViewModel(); } RootFrame.DataContext = ViewModel; }