[英]Windows Phone 8 - Navigation Error
我在IsolatedStorage
有一个文件。 如果该文件存在,我想重定向到登录页面或“创建帐户”页面。
如果文件不存在,则应用程序转到“创建”页面,创建并保存密码,然后将应用程序重定向到“登录”页面。 但是,如果IsolatedStorage中的文件存在,则不会定向。
private void fileExists()
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists("passwordFile"))
{
//NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
MessageBox.Show("Should be redirecting here");
}
else
{
MessageBox.Show("Welcome. Please create an account. Ensure that you remember your password!");
}
}
实际的消息确实显示出来,因此它正在被调用,如果文件不存在,则执行else,所以我的逻辑是合理的。
在此FileExists()
函数。
public MainPage()
{
InitializeComponent();
fileExists();
}
另一个重定向发生在这里
if ((password1.Password == password2.Password) & (password1.Password.Trim().Length > 0 || password2.Password.Trim().Length > 0))
{
byte[] PasswordByte = Encoding.UTF8.GetBytes(password1.Password);
byte[] ProtectedPassword = ProtectedData.Protect(PasswordByte, null);
this.WritePasswordToFile(ProtectedPassword);
NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
}
该错误是System.NullReferenceException
但未在用户代码中处理。
试图调用文件是否存在对MainPage加载的检查? 即使它正在执行,也可能是存储准备问题。 其次,如果您可以提及确切的异常发生位置。 另外,请检查此链接可能对您有帮助。
问题是NavigationService
仍然为null,您可以通过在重定向行上放置一个断点,将相同的代码放入MainPage.Loaded
事件中来验证它是否可以正常工作,(我希望它随后可以正常工作)
如我所料,这只是一个重定向页面,您可以在初始化中检查文件并保存uri以在类中进行重定向,并在加载页面时进行重定向
我需要将fileExists()从构造函数移动到新函数。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
fileExists();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.