大家追了上来,NullReferenceException异常的解决方案在一个连续的循环
与大多数的应用程序,如果设置了密码,IM中需要包含一个登录页面,我的应用程序。应用程序的预期行为是,只有在设置了密码的情况下,它才能导航到passwordpage.xaml,并输入正确的密码时,它应该导航到mainpage.xaml。如果密码没有设置,它应该直接导航到mainpage.xaml。
以下博客建议需要重新导航,以便在app.xaml.cs中包含一项检查,以确定应用需要导航到哪个页面。
但现在的问题是,密码页面的进一步导航没有发生。它在设置密码时导航到密码页面,但是在签入密码匹配后,它不会在mainpage.xaml上移动,而是会返回到rootframe_navigating事件处理程序并执行循环。
http://blogs.msdn.com/b/ptorr/archive/2010/08/28/redirecting-an-initial-navigation.aspx
这是app.xaml.cs
void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
//throw new NotImplementedException();
if (e.Uri.ToString().Contains("/MainPage.xaml") != true)
return;
CycleManager pCycMan = CycleManager.instance;
bool checkOk = false;
pCycMan.ReadFromIsolatedStorage();
if (pCycMan.GetPasswordEnabled())
{
checkOk = true;
}
e.Cancel = true;
RootFrame.Dispatcher.BeginInvoke(delegate
{
if (checkOk)
RootFrame.Navigate(new Uri("/PasswordPage.xaml", UriKind.Relative));
else
RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
});
}
的功能,这是在passwordpage.xaml
private void OnClick(object sender, RoutedEventArgs e)
{
CycleManager pCycMan = CycleManager.instance;
if (pCycMan.GetPassword() == passwordBox1.Password)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
else
{
MessageBox.Show("Incorrect Password");
}
}
任何书面上的按钮点击功能建议?
Alfah
+1这里。废弃您当前的方法,并在主页的'Loaded'或'OnNavigatedTo'方法中执行check/redirect to passwordpage。然后在设置密码时使用'GoBack()'。 –
@adam GoBack()不能被使用,因为在后端堆栈中没有条目,即它还没有导航到mainpage.xaml。它检查app.xaml中的条件,然后转到密码页面,然后移动到主页面。xaml但它不会移动到mainpage.xaml – alfah
@alfah这很可能是因为您试图取消导航事件并在同一代码块中启动新事件。尝试将密码页面设置为开始页面,以便您不需要劫持导航处理程序......用户第一眼看到的只是一个登录页面。然后,您可以向前导航并弹出后备堆栈中的密码页面,从此以后的所有内容都应该是正常行为。 –