3
votes

Alright. I'm developing a Windows 8 (WinRT) app with C# and XAML, and I have a listbox which contains quite a few elements fetched from JSON. When you click one of the items, you'll be transferred to a new page showing the whole news item. This works.

However, whenever I click an item and I'm transferred, and I click the backbutton on the phone, the app crashes. I'm getting the following error, and I have no idea why it doesn't work! Any ideas?

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));        
    }

Exception:

System.ArgumentOutOfRangeException was unhandled Message= Parameter name: index StackTrace: at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List1.get_Item(Int32 index) at System.Linq.Enumerable.ElementAt[TSource](IEnumerable1 source, Int32 index) at MunchApp3._0.MainPage.NewsList_SelectionChanged_1(Object sender, SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List1 unselectedItems, List1 selectedItems) at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ListBox.OnItemsChanged(NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource) at System.Windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue) at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet) at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value) at MunchApp3._0.MainPage.webClientNews_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Delegate.DynamicInvokeOne(Object[] args) at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) at System.Delegate.DynamicInvoke(Object[] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) at System.Windows.Threading.Dispatcher.OnInvoke(Object context) at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

1
Could you possibly copy that exception and post it as text in your question? While you're at it, post your code, too. Having to copy/paste the image is too much work. What is the value of NewsList.SelectedIndex when you enter the method?Jim Mischel
@JimMischel Tried breakpoint'ing from the method start, where it has index 0.AndreasB
And you're sure that there are items in the newslist.Json collection? Have you checked the Count property (or return value of the Count() method)?Jim Mischel
Yeah, it has two (2) items. And that's the weird thing, since newslistJson populates the Listbox upon the start of the app, which works. It only crashes after actually entering one of the listbox items (which works), and then returning to the MainPage.xaml page, where it crashes.AndreasB
So when the exception is thrown, SelectedIndex is 0, and newslist.Json contains 2 items? That's ... unusual. How about putting a test in the code that says, if (NewsList.SelectedIndex < 0 || NewsList.SelectedIndex >= newslist.Json.Count) { throw new Exception(); } ... put a breakpoint on the throw and see what happens when you run your program.Jim Mischel

1 Answers

0
votes

try with

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