3

好吧。我正在使用 C# 和 XAML 开发一个 Windows 8 (WinRT) 应用程序,并且我有一个列表框,其中包含从 JSON 获取的相当多的元素。当您单击其中一个项目时,您将被转移到显示整个新闻项目的新页面。这行得通。

但是,每当我单击一个项目并转移,然后单击手机上的后退按钮时,应用程序就会崩溃。我收到以下错误,我不知道为什么它不起作用!有任何想法吗?

参数OutOfRangeException

        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= 参数名称:索引 StackTrace:在 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(列表1 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(Object sender, NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource) 在 System.Windows.Controls System.Windows.DependencyObject 处的 .ItemsControl.ItemsSourceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)。在 System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty 属性、EffectiveValueEntry oldEntry、EffectiveValueEntry& newEntry、ValueOperation 操作)在 System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp、对象值、布尔值 allowReadOnlySet)的 RaisePropertyChangeNotifications(DependencyProperty dp、Object oldValue、Object newValue)在 System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value) 在 System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) 在 System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value) 在 MunchApp3._0.MainPage .webClientNews_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e) 在 System.Net.WebClient。OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) 在 System.Net.WebClient.DownloadStringOperationCompleted(Object arg) 在 System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfoculture, Boolean isBinderDefault, Assembly caller, System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化, StackCrawlMark& stackMark) 在 System.Reflection.MethodBase.Invoke(Object obj, Object[] 的布尔 verifyAccess, StackCrawlMark& stackMark)参数)在 System.Delegate.DynamicInvokeOne(Object[] args) 在 System.MulticastDelegate.DynamicInvokeImpl(Object[] args) 在 System.Delegate。DynamicInvoke(Object[] args) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) 在 System.Windows.Threading.Dispatcher.OnInvoke(Object context) 在 System。 Windows.Hosting.CallbackCookie.Invoke(Object[] args) 在 System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) 在 System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams , ScriptParam & pResult)在 System.Windows.Hosting.DelegateWrapper.Invoke(Object[] args) 在 System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam & pResult)在 System.Windows.Hosting.DelegateWrapper.Invoke(Object[] args) 在 System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam & pResult)

4

1 回答 1

0

尝试

Dispatcher.BeginInvoke(() =>
{
   NavigationService.Navigate(new Uri("/NewsPage.xaml?language=" + chosenLanguage, UriKind.Relative));
});  
于 2013-06-02T19:11:00.323 回答