我正在开发Windows Phone 8+应用程序。 我有一个包含项目详细信息的页面。每个Item都可以是另一个Item的一个组件,因此我有两个ListBox,其中包含可以组成或组成当前Item的其他Items。
JItem.Item currItem;
List<JItem.Item> components;
List<JItem.Item> isComponentOf;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
currItemId = NavigationContext.QueryString["item_id"];
currItem = (JItem.Item)App.itemsDict[currItemId];
LayoutRoot.DataContext = currItem;
components = new List<JItem.Item>();
isComponentOf = new List<JItem.Item>();
foreach (var x in currItem.components)
{
components.Add((JItem.Item)App.itemsDict[x]);
}
foreach (var x in currItem.composes)
{
isComponentOf.Add((JItem.Item)App.itemsDict[x]);
}
componentsLB.ItemsSource = components;
composesLB.ItemsSource = isComponentOf;
if (components.Count == 0)
componentsLabel.Visibility=Visibility.Collapsed;
if (isComponentOf.Count == 0)
isComponentLabel.Visibility=Visibility.Collapsed;
}
如果我向前导航选择componentsLB
列表框或isComponentOf
列表框中的项目,则一切正常。如果我通过“返回”按钮返回页面,即使System.NullReferenceException
包含项目并且componentsLB.ItemsSource = components;
已分配,我也会在components
行上显示componentsLB
。< / p>
由于它在NavigationStack上,因此页面不会重新初始化,(不会调用构造函数中的InitializeComponent()
),但会分配ListBox。
当通过后退导航访问页面时,我可以使用一些变通方法(不是重新加载数据),但我想了解导致错误的原因。
这是异常的Stacktrace:起泡:
at DOTA2_Pocket.ItemDetails.componentsLB_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 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.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(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.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
at DOTA2_Pocket.ItemDetails.OnNavigatedTo(NavigationEventArgs e)
at Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedTo(NavigationEventArgs e)
at Microsoft.Phone.Controls.PhoneApplicationPage.Microsoft.Phone.Controls.IPhoneApplicationPage.InternalOnNavigatedToX(NavigationEventArgs e)
at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, IPhoneApplicationPage existingContentPage, IPhoneApplicationPage newContentPage)
at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content, NavigationMode mode)
at System.Windows.Navigation.NavigationService.<>c__DisplayClass7.<NavigateCore_StartNavigation>b__4()
答案 0 :(得分:0)
根据您提供的信息,您的 App.itemsDict 包含您要添加到组件列表变量的空引用。 ListBox的 SelectedChanged 事件然后抛出 NullReference 异常,因为它试图对该空引用执行操作。