我得到了帶有列表框(列表框從可觀察集合中取得項目)的屏幕,用戶可以在其中選擇項目。Observable Collection和System.ArgumentOutOfRangeException
這是顯示了什麼情況的代碼時,關於該項目的用戶點擊:
// method is invoked when we select the element from the list
private void ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// getting object which was clicked in the listbox
dbFoodTable item = Fooditems.ElementAt(ItemsList.SelectedIndex);
// mark this object as choosenMeal and store it to get it back in next page
PhoneApplicationService.Current.State["choosen"] = item;
//going to next page
NavigationService.Navigate(new Uri("Customization.xaml", UriKind.Relative));
}
當我選擇從列表框,我去下一個屏幕,在這裏我有這個項目的所有細節(如獎金項目,說明,名稱...),錯誤顯示我支持此列表框屏幕時。
此行
dbFoodTable item = Fooditems.ElementAt(ItemsList.SelectedIndex);
是強調了與該信息類型「System.ArgumentOutOfRangeException」的異常出現在mscorlib.ni.dll但在用戶代碼中沒有處理。此行在
ItemsList_SelectionChanged
方法。
這將是完美的,如果這種方法甚至不會被援引,當我支持,但世界並不完美。 我認爲它是這樣工作的,當我們轉到其他頁面時,例如我們選擇的第二個項目,當我們支持此屏幕時,此項目不再被選中=> selectionchanged =>調用方法
我該如何解決這個問題 ?
我懷疑'ListBox'沒有選擇任何東西,因此'SelectedIndex'是-1。只需在條件中檢查這一點,不要執行你的代碼。 – Steve
是的,當然,我應該這樣做,如果聲明.. thx求助,寫它作爲答案,我會接受它 – MyWay