我有一个简单的待办事项列表,它存储在列表对象中,通过数据绑定通过列表框显示。这些项目是可点击的,它们会将您带到详细信息页面,您还可以在其中删除任务。一切正常,但是当我单击后退按钮时,我得到一个 ArgumentOutOfRangeException 并且应用程序崩溃。
现在,我认为这是因为应用程序认为列表中仍有项目,但我不太确定如何修复它。
System.Windows.dll 上引发了异常 - 没有具体内容。另外,我知道在 OnNavigatedTo 事件之后引发了异常。
XAML(只是列表框 - 有点简化):
<ListBox Name="MyActivitiesList" ItemsSource="{Binding MyActivities}" SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<StackPanel Margin="0,0,0,12">
<-- context menu was here, but has been removed -->
<TextBlock Text="{Binding Title}" Style="{StaticResource BigHeader}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Category.Name}" Style="{StaticResource SubtleText}"/>
<TextBlock Text=" - " Style="{StaticResource SubtleText}"/>
<TextBlock Text="{Binding VotesPercentage}" Style="{StaticResource SubtleText}"/>
<TextBlock Text="% positive" Style="{StaticResource SubtleText}" />
</StackPanel>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
代码隐藏:
private MyActivitiesViewModel myActivitiesViewModel;
public MyActivitiesView()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
myActivitiesViewModel = new MyActivitiesViewModel();
this.DataContext = myActivitiesViewModel;
base.OnNavigatedTo(e);
}
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
MyActivitiesList.SelectedItem = null;
base.OnNavigatingFrom(e);
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((ListBox)sender).SelectedItem != null)
{
var idea = ((ListBox)sender).SelectedItem as Idea;
((ListBox)sender).SelectedItem = null;
Dispatcher.BeginInvoke(() => NavigationUtility.Navigate(new Uri("/View/IdeaView.xaml", UriKind.Relative), idea));
}
}
有任何想法吗?帮助将不胜感激!