protected override void OnNavigatedFrom(NavigationEventArgs e) { if (((App)App.Current).IsSuspending) { // Save volatile state in case we get terminated later on // then we can restore as if we'd never been gone ApplicationDataContainer Item = ApplicationData.Current.LocalSettings.CreateContainer("Item", ApplicationDataCreateDisposition.Always); if (ApplicationData.Current.LocalSettings.Containers.ContainsKey("Item")) { Item.Values["title"] = title.Text; ITextRange range = detail.Document.GetRange(0, TextConstants.MaxUnitCount); Item.Values["detail"] = range.Text; Item.Values["date"] = ddl.Date; Item.Values["imgname"] = Common.selectName; Item.Values["btn"] = create_update.Content; } if (ViewModel.SelectedItem != null) { ApplicationData.Current.LocalSettings.Values["selectitem"] = ViewModel.getItems.IndexOf(ViewModel.SelectedItem); } List <string> L = new List <string>(); var allitems = ViewModel.getItems; foreach (var a in allitems) { var item = new myItem(a.date, a.imgname, a.title, a.detail, a.finish); L.Add(JsonConvert.SerializeObject(item)); } ApplicationData.Current.LocalSettings.Values["allitems"] = JsonConvert.SerializeObject(L); } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed; dataTransferManager.DataRequested += DataRequested; if (e.NavigationMode == NavigationMode.New) { // If this is a new navigation, this is a fresh launch so we can discard any saved state ApplicationData.Current.LocalSettings.Values.Remove("Item"); ApplicationData.Current.LocalSettings.Values.Remove("allitems"); ApplicationData.Current.LocalSettings.Values.Remove("selectitem"); } else { // Try to restore state if any, in case we were terminated if (ApplicationData.Current.LocalSettings.Values.ContainsKey("allitems")) { ViewModel.getItems.Clear(); List <string> L = JsonConvert.DeserializeObject <List <string> >( (string)ApplicationData.Current.LocalSettings.Values["allitems"]); foreach (var l in L) { myItem a = JsonConvert.DeserializeObject <myItem>(l); TodoItem item = new TodoItem(a.date.Date, a.imgname, a.title, a.detail, a.finish); ViewModel.getItems.Add(item); } } if (ApplicationData.Current.LocalSettings.Values.ContainsKey("selectitem")) { ViewModel.SelectedItem = ViewModel.getItems[(int)(ApplicationData.Current.LocalSettings.Values["selectitem"])]; } if (ApplicationData.Current.LocalSettings.Containers.ContainsKey("Item")) { ApplicationDataContainer Item = ApplicationData.Current.LocalSettings.Containers["Item"]; create_update.Content = Item.Values["btn"] as string; title.Text = Item.Values["title"] as string; ITextRange range = detail.Document.GetRange(0, TextConstants.MaxUnitCount); range.Text = Item.Values["detail"] as string; ddl.Date = (DateTimeOffset)(Item.Values["date"]); Common.selectName = Item.Values["imgname"] as string; if (Common.selectName == "") { pic.Source = new BitmapImage(new Uri("ms-appx:///Assets/todo.png")); } else { var file = await ApplicationData.Current.LocalFolder.GetFileAsync(Common.selectName); IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read); BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); pic.Source = bitmapImage; } } } }