I allow the user to delete the item through the ContextMenu menu item in the view, and I would like the effect to appear automatically in the view. At the moment, the element in the view remains until the application is restarted, in which case it will no longer be visible. This is very confusing for the user who believes that the item is completely removed from the application. My question is: what am I doing wrong to solve this problem?
App.xaml.cs
//PictureRepository is a class that gathers pictures from IsolatedStorage public static PictureRepository PictureList { get { return PictureRepository.Instance; } }
PictureRepository.cs
#region Constants public const string IsolatedStoragePath = "Pictures";
MainPage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e) { //Recent is the ListBox in the view that is populated from the ObservableCollection if (Settings.AscendingSort.Value) { //Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderBy(x => x.DateTaken); Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken); } else { //Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderByDescending(x => x.DateTaken); Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(x => x.DateTaken); } } //My attempt at deleting the item from both the View and IsolatedStorage private void deleteContextMenuItem_Click(object sender, RoutedEventArgs e) { var listBoxItem = ((MenuItem)sender).DataContext as CapturedPicture; fileName = listBoxItem.FileName; if (fileName != null) { try { //Neither of the following remove the item from the View immediately App.PictureList.Pictures.Remove(listBoxItem); PictureRepository.Instance.Pictures.Remove(listBoxItem); //To open the file, add a call to the IsolatedStorageFile.GetUserStoreForApplication var isoFile = IsolatedStorageFile.GetUserStoreForApplication(); //Combine the directory and file name string filePath = Path.Combine(PictureRepository.IsolatedStoragePath, fileName); isoFile.DeleteFile(filePath); } catch { MessageBoxResult r = MessageBox.Show("There was an error deleting the image.", "Notice", MessageBoxButton.OK); if (r == MessageBoxResult.OK) return; } } }
As you can see, I am trying to remove an item from View and IsolatedStorage. At the moment, removing from IsolStorage works, but I can't get the view to automatically refresh. I need to wait until the application is restarted and the ListBox will be re-populated in the OnNavigatedTo event to see any changes.
source share