【发布时间】:2013-03-06 15:54:07
【问题描述】:
好的。我正在使用 C# 和 XAML 开发一个 Windows 8 (WinRT) 应用程序,并且我有一个列表框,其中包含从 JSON 获取的相当多的元素。当您单击其中一个项目时,您将被转移到显示整个新闻项目的新页面。 这行得通。
但是,每当我单击一个项目并转移,然后单击手机上的后退按钮时,应用程序就会崩溃。我收到以下错误,我不知道为什么它不起作用!有任何想法吗?
private void NewsList_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
index = NewsList.SelectedIndex;
NewsItems newsContentGetSet = new NewsItems();
newsContentGetSet.news_id = newslistJson.ElementAt(index).news_id;
newsContentGetSet.news_title = newslistJson.ElementAt(index).news_title;
newsContentGetSet.news_abstract = newslistJson.ElementAt(index).news_abstract;
newsContentGetSet.news_content = newslistJson.ElementAt(index).news_content;
newsContentGetSet.news_author = newslistJson.ElementAt(index).news_author;
newsContentGetSet.news_date_formatted = newslistJson.ElementAt(index).news_date_formatted;
newsContentGetSet.user_firstname = newslistJson.ElementAt(index).user_firstname;
newsContentGetSet.user_lastname = newslistJson.ElementAt(index).user_lastname;
App.newsContentGetSet = newsContentGetSet;
NavigationService.Navigate(new Uri("/NewsPage.xaml?language=" + chosenLanguage, UriKind.Relative));
}
例外:
System.ArgumentOutOfRangeException 未处理 Message= 参数 名称:索引堆栈跟踪: 在 System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument 参数,ExceptionResource 资源) 在 System.ThrowHelper.ThrowArgumentOutOfRangeException() 在 System.Collections.Generic.List
1.get_Item(Int32 index) at System.Linq.Enumerable.ElementAt[TSource](IEnumerable1 源,Int32 索引) 在 MunchApp3._0.MainPage.NewsList_SelectionChanged_1(对象发送者,SelectionChangedEventArgs e) 在 System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e) 在 System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List1 unselectedItems, List1 selectedItems) 在 System.Windows.Controls.Primitives.Selector.SelectionChanger.End() 在 System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.ListBox.OnItemsChanged(NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.ItemsControl.OnItemCollectionChanged(对象 发件人,NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable 新项目源) 在 System.Windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d、DependencyPropertyChangedEventArgs e) 在 System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, 对象 oldValue, 对象 newValue) 在 System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty 属性,EffectiveValueEntry oldEntry,EffectiveValueEntry&newEntry, 值操作操作) 在 System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp,对象值,布尔值 allowReadOnlySet) 在 System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp,对象值) 在 System.Windows.DependencyObject.SetValue(DependencyProperty dp,对象值) 在 System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable 价值) 在 MunchApp3._0.MainPage.webClientNews_DownloadStringCompleted(对象 发件人,DownloadStringCompletedEventArgs e) 在 System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) 在 System.Net.WebClient.DownloadStringOperationCompleted(对象 arg) 在 System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object 参数,CultureInfo 文化,布尔 isBinderDefault,程序集 调用者、布尔验证访问、StackCrawlMark 和 stackMark) 在 System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化、StackCrawlMark 和 stackMark) 在 System.Reflection.MethodBase.Invoke(对象 obj,对象 [] 参数) 在 System.Delegate.DynamicInvokeOne(Object[] args) 在 System.MulticastDelegate.DynamicInvokeImpl(对象 [] 参数) 在 System.Delegate.DynamicInvoke(对象 [] 参数) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority 优先) 在 System.Windows.Threading.Dispatcher.OnInvoke(对象上下文) 在 System.Windows.Hosting.CallbackCookie.Invoke(对象 [] 参数) 在 System.Windows.Hosting.DelegateWrapper.InternalInvoke(对象 [] 参数) 在 System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle,Int32 nParamCount,ScriptParam[] pParams,ScriptParam& p结果)
【问题讨论】:
-
您能否复制该异常并将其作为文本发布在您的问题中?当您使用它时,也可以发布您的代码。必须复制/粘贴图像的工作量太大。输入方法时
NewsList.SelectedIndex的值是多少? -
@JimMischel 尝试从方法开始断点,它的索引为 0。
-
您确定
newslist.Json集合中有项目吗?你检查过Count属性(或Count()方法的返回值)吗? -
是的,它有两 (2) 项。这很奇怪,因为 newslistJson 在应用程序启动时填充列表框,这很有效。它只会在实际输入列表框项目之一(有效)后崩溃,然后返回到 MainPage.xaml 页面,它会在该页面崩溃。
-
那么当抛出异常时,
SelectedIndex为0,而newslist.Json包含2项?那是……不寻常。在if (NewsList.SelectedIndex < 0 || NewsList.SelectedIndex >= newslist.Json.Count) { throw new Exception(); }的代码中放置一个测试怎么样...在throw上放置一个断点,看看运行程序时会发生什么。
标签: c# json xaml windows-runtime windows-store-apps