ListBox 上的 NullReferenceException(不同页面)

NullReferenceException on ListBox(different page)

提问人:Ahmed.C 提问时间:11/21/2013 最后编辑:Anton SizikovAhmed.C 更新时间:11/21/2013 访问量:96

问:

所以我遇到了这个问题,我有 2 个页面,ViewAccount 和 MainPage。

在 MainPage 中,有一个名为 SavedAccountsLst 的 ListBox。问题是,在ViewAccount页面中,一旦我单击一个按钮,它就会抛出一个.NullReferenceException

这是我在ViewAccount中使用的代码:

Private Sub Delete_Account_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim Storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Storage.DeleteFile("SafeLockPLUS/SavedAccounts/" & PageTitle.Text)
    MessageBox.Show(PageTitle.Text & " has been deleted.", "Deleted", MessageBoxButton.OK)
    Dim aa = TryCast(DirectCast(Application.Current, App).RootFrame.Content, MainPage)
    aa.SavedAccountslst.ItemsSource = Nothing
    Dim directory As String = "./SafeLockPLUS/SavedAccounts/*.*"
    Dim filenames As String() = Storage.GetFileNames(directory)
    aa.SavedAccountslst.ItemsSource = filenames
    NavigationService.GoBack()

End Sub

它的作用是,它将从 viewaccount 页面中删除文件,并清除 SavedAccountList 中的项目,并在 ViewAccount 页面中重新加载它们,然后返回。

vb.net windows-phone-7 列表框 nullreferenceexception

评论

0赞 Anton Sizikov 11/21/2013
究竟是哪一行抛出异常?
0赞 Ahmed.C 11/21/2013
机 管 局。SavedAccountslst.ItemsSource = 无
0赞 Kevin Gosse 11/21/2013
如果您在 ViewAccount 中,则 是 ,而不是 ,因此会出现错误App.RootFrame.ContentViewAccountMainPage

答:

0赞 Anton Sizikov 11/21/2013 #1

您在 上时执行此代码,基本上这意味着它包含一个页面,而不是一个 .ViewAccountPageRootFrame.ContentViewAccountPageMainPage

Dim aa = TryCast(DirectCast(Application.Current, App).RootFrame.Content, MainPage)

您正在尝试强制转换,但强制转换失败,因此您在变量中获得了 nullRootFrame.ContentMainPageaa

评论

0赞 Ahmed.C 11/21/2013
我无法访问 ViewAccount,因为我已经在 ViewAccount 中,我添加的唯一原因:Dim aa = TryCast(DirectCast(Application.Current, App)。RootFrame.Content, MainPage) 是进行某种引用,就像在 ViewAccount Page 中我调用 aa.SavedAccountslst.ItemsSource = Nothing,告知从 viewaccount 页面清除名为 Savedaccountslst 的列表框。
0赞 Anton Sizikov 11/21/2013
您清楚 RootFrame.Content 中没有 MainPage?
0赞 Anton Sizikov 11/21/2013
您正在访问 RootFrame.Current,但您是否了解其中有一个 ViewAccountPage?