ListBox上的NullReferenceException(不同页面)

时间:2013-11-21 08:59:35

标签: vb.net windows-phone-7 listbox nullreferenceexception

所以我遇到了这个问题,我有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页面中重新加载它们然后返回。

1 个答案:

答案 0 :(得分:0)

您在ViewAccountPage时执行此代码,基本上这意味着RootFrame.Content包含ViewAccountPage页面,而不是MainPage

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

您正在尝试将RootFrame.Content投射到MainPage并且您的广告投放失败,因此您在aa变量

中变为空