使用类方法时无法导航到页面

时间:2015-03-12 02:06:52

标签: navigation windows-runtime windows-phone-8.1 win-universal-app

我正在使用Windows Phone 8.1通用应用程序,我在其中创建了一个类方法,并将其放在我的应用程序中某个页面的NavigationHelper_LoadState方法中。我的导航如下,我单击我的主页上的链接,然后将我带到有问题的页面,我将类方法放在LoadState中。

class方法检查用户的身份验证状态。如果用户未登录,则应该将他带到单独的登录页面(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;
            }
        }
    }
}

另外,我在我的班级中将rootFrame定义如下:

 Frame rootFrame = Window.Current.Content as Frame;

我从LoadState方法调用类方法,如下所示:

 SHD_helper.SHDAuthState(errorMessage, pgbar, pgText, ServerNetworkError);

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为在加载第一页之前调用Navigate方法......

您是否尝试将该方法置于新的调度程序操作中?

rootFrame.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    delegate
    {
        rootFrame.Navigate(typeof (SHDSignIn));
    });

但我认为你不应该在LoadState回调中检查这个......