WP7页面导航.Null异常



我正在制作该应用程序,它涉及使用文本文件等创建本地帐户。在第一次运行时,该应用程序旨在导航到名为"MainPage"的页面,但是,如果该文件夹中不存在文件"FTR.dat",则它将导航到"CreateAccount"页面,但如果该特定文件夹中确实存在文件"FTIR.dat

但是我得到了这个nullreferenceexception错误:这是我的代码:

  Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    If myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = False Then
        NavigationService.Navigate(New Uri("/CreateAccount.xaml", UriKind.Relative))
    ElseIf myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = True Then
        NavigationService.Navigate(New Uri("/MainPage.xaml", UriKind.Relative))
    End If

谢谢!

您不能从页面构造函数调用此类代码,因为导航服务尚未初始化。将其移动到Loaded事件或OnNavigatedTo:

Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
    Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    If myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = False Then
        NavigationService.Navigate(New Uri("/CreateAccount.xaml", UriKind.Relative))
    ElseIf myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = True Then
        NavigationService.Navigate(New Uri("/MainPage.xaml", UriKind.Relative))
    End If
End Sub

最新更新