rams
rams

Reputation: 652

how to resolve unhandled exception when i press back key of device?

I got unhandled exception when i press back key of device how to resolve this problem ?

I have implemented favorites feature in windows phone 7 application . private void FavoriteClick(object sender, EventArgs e) {

            var favorites = GetFavorites();

            if (favorites.Any(m => m.key == _key))
            {
                RemoveFavorite();
                IsolatedStorageSettings.ApplicationSettings["favorites"] = favorites;
                //NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
                return;
            }

            AddFavorite();              
    }

    private void AddFavorite()
    {
        const string messageBoxText = "Do you wish to add this page to your favorites?";
        const string caption = "Add Favorite";
        const MessageBoxButton button = MessageBoxButton.OKCancel;
        // Display message box
        var result = MessageBox.Show(messageBoxText, caption, button);

        // Process message box results
        switch (result)
        {
            case MessageBoxResult.OK:
                var favorites = GetFavorites();
                favorites.Add(_page);                   
                IsolatedStorageSettings.ApplicationSettings["favorites"] = favorites;
                break;
        }
    }

    private void RemoveFavorite()
    {
        const string messageBoxText = "Do you wish add remove this page to your favorites?";
        const string caption = "Remove Favorite";
        const MessageBoxButton button = MessageBoxButton.OKCancel;
        // Display message box
        MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button);

        // Process message box results
        switch (result)
        {
            case MessageBoxResult.OK:
                List<MobiRecord> favorites = GetFavorites();

                foreach (MobiRecord m in favorites)
                {
                    if (m.key == _key)
                    {
                        favorites.Remove(m);
                        IsolatedStorageSettings.ApplicationSettings["favorites"] = favorites;                           
                        return;
                    }
                }                    
                break;
        }
    }

Problem :

i have added some of favorites after i go to favorites page then select any one added favorites then click remove favorite after i click back button the application automatically closed(i got unhanded exception).

Upvotes: 0

Views: 215

Answers (1)

Wolfgang Ziegler
Wolfgang Ziegler

Reputation: 1685

The problem here is most likely that your are changing the collection (by calling Remove on favorites) over which your are doing the foreach iteration. This will cause your exception (although I would need exception Details to be really sure).

Just as this code snippet for removing from the collection:

favorites.RemoveAll(m => m.key == _key);

Upvotes: 1

Related Questions