我正在制作应用程序,它涉及使用文本文件等创建本地帐户。首次运行时,该应用程序将导航到名为“MainPage”的页面,但是,如果文件“FTR.dat”没有t存在于此特定文件夹中,然后它将导航到“CreateAccount”页面,但如果文件“FTR.dat”确实存在于该特定文件夹中,那么它将导航到“MainPage”
但是我得到了这个nullrefrenceexception错误: 这是我的代码:
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
谢谢!
答案 0 :(得分:1)
您无法从页面构造函数中调用此类代码,因为导航服务尚未初始化。将其移至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