C# 当我按下设备的后退键时,如何解决未处理的异常?
当我按下设备的“后退”键时,出现未处理的异常。如何解决此问题 我已经在WindowsPhone7应用程序中实现了收藏夹功能。 私有void收藏夹单击(对象发送者,事件参数) {C# 当我按下设备的后退键时,如何解决未处理的异常?,c#,windows-phone-7,C#,Windows Phone 7,当我按下设备的“后退”键时,出现未处理的异常。如何解决此问题 我已经在WindowsPhone7应用程序中实现了收藏夹功能。 私有void收藏夹单击(对象发送者,事件参数) { var favorites = GetFavorites(); if (favorites.Any(m => m.key == _key)) { RemoveFavorite();
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;
}
}
var favorites=GetFavorites();
if(favorites.Any(m=>m.key==\u key))
{
删除收藏夹();
隔离存储设置。应用程序设置[“收藏夹”]=收藏夹;
//NavigationService.Navigate(新Uri(“/MainPage.xaml”,UriKind.Relative));
返回;
}
AddFavorite();
}
私有void AddFavorite()
{
const string messageBoxText=“是否要将此页面添加到收藏夹?”;
const string caption=“添加收藏夹”;
const MessageBoxButton=MessageBoxButton.ok取消;
//显示消息框
var result=MessageBox.Show(MessageBox文本、标题、按钮);
//处理消息框结果
开关(结果)
{
case MessageBoxResult.OK:
var favorites=GetFavorites();
收藏夹。添加(_页);
隔离存储设置。应用程序设置[“收藏夹”]=收藏夹;
打破
}
}
私有void RemoveFavorite()
{
const string messageBoxText=“是否要将此页面添加到收藏夹中?”;
const string caption=“删除收藏夹”;
const MessageBoxButton=MessageBoxButton.ok取消;
//显示消息框
MessageBoxResult=MessageBox.Show(messageBoxText、标题、按钮);
//处理消息框结果
开关(结果)
{
case MessageBoxResult.OK:
列出收藏夹=GetFavorites();
foreach(收藏夹中的MobiRecord m)
{
如果(m.key==\u key)
{
收藏夹。删除(m);
隔离存储设置。应用程序设置[“收藏夹”]=收藏夹;
返回;
}
}
打破
}
}
问题:
进入收藏夹页面后,我添加了一些收藏夹,然后选择任何一个已添加的收藏夹,然后单击“上一步”按钮后单击“删除收藏夹”应用程序将自动关闭(我未遇到意外异常)。这里的问题很可能是您正在更改收藏(通过调用“删除收藏夹”)您正在进行foreach迭代。 这将导致您的异常(尽管我需要异常详细信息才能真正确定) 与从集合中删除的代码片段相同:
favorites.RemoveAll(m => m.key == _key);
您是否设置了任何断点?这些断点是否在引发未处理的异常之前命中?异常+调用堆栈是什么?如果您确实需要正确的解决方案,请添加异常详细信息,包括调用堆栈