SelectionChanged事件WinRT中的ArgumentOutOfRangeException

时间:2013-03-06 15:54:08

标签: c# json xaml windows-runtime windows-store-apps

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

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

ArgumentOutOfRangeException

        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 = Parameter   name:index StackTrace:          在System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument)   参数,ExceptionResource资源)          在System.ThrowHelper.ThrowArgumentOutOfRangeException()          在System.Collections.Generic.List 1.get_Item(Int32 index) at System.Linq.Enumerable.ElementAt[TSource](IEnumerable 1源,Int32索引)          在MunchApp3._0.MainPage.NewsList_SelectionChanged_1(对象发送者,SelectionChangedEventArgs e)          在System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs)   E)          在System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List 1 unselectedItems, List 1 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.ItemsControl.ItemsSourceChanged(DependencyObject)   d,DependencyPropertyChangedEventArgs e)          在System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty)   dp,Object oldValue,Object newValue)          在System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty   property,EffectiveValueEntry oldEntry,EffectiveValueEntry&新条目,   ValueOperation操作)          在System.Windows.DependencyObject.SetValueInternal(DependencyProperty   dp,Object value,Boolean allowReadOnlySet)          在System.Windows.DependencyObject.SetValueInternal(DependencyProperty   dp,对象值)          在System.Windows.DependencyObject.SetValue(DependencyProperty dp,Object value)          在System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable   值)          在MunchApp3._0.MainPage.webClientNews_DownloadStringCompleted(对象   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   参数,CultureInfo文化,布尔isBinderDefault,汇编   调用者,布尔验证访问,StackCrawlMark& stackMark)          在System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,   CultureInfo culture,StackCrawlMark& stackMark)          在System.Reflection.MethodBase.Invoke(Object obj,Object []参数)          在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   优先)          在System.Windows.Threading.Dispatcher.OnInvoke(对象上下文)          在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)

1 个答案:

答案 0 :(得分:0)

尝试

Dispatcher.BeginInvoke(() =>
{
   NavigationService.Navigate(new Uri("/NewsPage.xaml?language=" + chosenLanguage, UriKind.Relative));
});