[英]how to resolve unhandled exception when i press back key of device?
按下設備的返回鍵時出現未處理的異常,該如何解決?
我已經在Windows Phone 7應用程序中實現了收藏夾功能。 私人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;
}
}
問題:
我轉到“收藏夾”頁面后添加了一些收藏夾,然后選擇任何一個添加的收藏夾,然后在單擊“后退”按鈕后單擊“刪除收藏夾”,應用程序自動關閉(我收到了未處理的異常)。
這里的問題很可能是您正在更改要進行foreach迭代的集合(通過在收藏夾上調用Remove)。 這將導致您的異常(盡管我確實需要異常詳細信息才能確定)。
就像從集合中刪除的以下代碼片段一樣:
favorites.RemoveAll(m => m.key == _key);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.