【发布时间】:2014-09-23 06:07:54
【问题描述】:
我正在开发一个 Windows Phone 8+ 应用程序。 我有一个包含项目详细信息的页面。每个项目都可以是另一个项目的组件,因此我有两个列表框,其中包含可以组合或构成当前项目的其他项目。
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 ListBox 或 isComponentOfListBox 中的项目,一切正常。如果我通过 Back 按钮返回到一个页面,我会在 componentsLB.ItemsSource = components; 行上看到一个 System.NullReferenceException,即使 components 包含项目并且分配了 componentsLB。
因为它在 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()
【问题讨论】:
-
调试器是你的朋友。 “Debug, Exceptions...”,检查“CLR 异常”,在调试器下运行你的应用,重现问题,然后从调用堆栈中你会发现你引用了哪个 null。
-
让我们看看堆栈跟踪!在函数开头设置断点并单步执行!
-
当然调试是我尝试的第一件事,但我似乎无法完全弄清楚错误在哪里。我将堆栈跟踪添加到我原来的问题中。
标签: c# windows-phone-8 windows-phone