我正在开发Windows Phone 8.1通用应用程序,其中我创建了一个类方法并将其放置在应用程序中其中一个页面的NavigationHelper_LoadState方法中。我的导航如下,我单击主页上的链接,这将我带到有问题的页面,我将类方法放置在 LoadState 中。
类方法检查用户的身份验证状态。如果用户没有登录,它应该将他带到一个单独的登录页面(SHDSignIn来自下面的代码片段)。
我遇到的问题是,当我在类方法中点击那部分代码时,它只是逐步执行重定向代码,但不会将我带到登录页面,而是将我带到从主页单击的页面。
从我到目前为止所做的故障排除似乎是一个问题,可能是因为我从NavigationHelper_LoadState调用该方法并且系统不喜欢它??有人可以解释一下并提供解决方法吗?
这是我的类函数代码:
public async void SHDAuthState(string errormessage, ProgressBar myprogressbar, TextBlock mytextblock, TextBlock myservernetworkerror)
{
//Get the values for the userID and password from the settings....
string shdLoggedInValue = (string)appRoamingSettings.Values["shdLoggedIn"];
//If not logged in, redirect to the SHD sign in page...
if (shdLoggedInValue != "Yes")
{
this.rootFrame.Navigate(typeof(SHDSignIn));
}
//Getting the cookie if it has expired..
else
{
//Get the cookie value...
string myCookieValue = (string)appRoamingSettings.Values["MyCookie"];
//Get the original cookie obtain time....
long CookieObtainedTimeValue = (long)appRoamingSettings.Values["CookieObtainedTime"];
//Convertig date/time back to DateTime object....
origCookieObtainedTime = DateTime.FromBinary(CookieObtainedTimeValue);
currentDateTime = DateTime.Now;
//Check to see if cookie has expired....
cookieTimeElasped = currentDateTime - origCookieObtainedTime;
cookieTimeElapsedMins = cookieTimeElasped.TotalMinutes;
// 2 days = 2880 mins but we give a margin of 1 minute
//Get a new cookie if it has expired and save to settings
if (cookieTimeElapsedMins >= 2879)
{
// Start showing the progress bar...
mycontrols.progressbarShow(myprogressbar, mytextblock);
//Get the values for the userID and password from the settings....
string UserIDValue = (string)appRoamingSettings.Values["UserID"];
string PasswordValue = (string)appRoamingSettings.Values["Password"];
//Update the requestData string before sending.....
requestData = "{" + string.Format(RegisterRequestData, UserIDValue, PasswordValue) + "}";
string registerResults = await SHDAPI(registerUrl, requestData, errormessage);
if (registerResults != null)
{
// Get the cookie and the time and save it to settings
var shdCookie = JsonConvert.DeserializeObject<SHDHelper.SHDObject>(registerResults).RegistrationCookie;
//Save cookie to the app settings
appRoamingSettings.Values["MyCookie"] = shdCookie;
//*************************************
// build the UI
//*************************************
// Stop showing the progress bar...
mycontrols.progressbarNoShow(myprogressbar, mytextblock);
}
else
{
// Stop showing the progress bar...
mycontrols.progressbarNoShow(myprogressbar, mytextblock);
//Show the error message...
myservernetworkerror.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
}
}
另外,我在类中定义了如下根框架:
Frame rootFrame = Window.Current.Content as Frame;
我从 LoadState 方法调用类方法,如下所示:
SHD_helper.SHDAuthState(errorMessage, pgbar, pgText, ServerNetworkError);
谢谢!
我认为Navigate
方法甚至在加载第一页之前就被调用了......
您是否尝试过将该方法放入新的调度程序操作中?
rootFrame.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
delegate
{
rootFrame.Navigate(typeof (SHDSignIn));
});
但我认为你不应该在 LoadState 回调中检查这一点......