2015-02-23 68 views
0

我想創建一個方法,可以在我的代碼中用於windows phone 8.1通用運行時應用程序的多個頁面上重複使用。所以我將這個片段從其中一個頁面複製到我的幫助類中。但是,我正在努力與「框架」類。我還定義了「框架」;最初我的方法之外,擺脫了我最初複製代碼時看到的問題。在爲windows phone 8.1創建方法時出現幀問題

以下是方法的代碼片段:

 public async void CheckAuthState(string pagename, string errormessage) 
 
     { 
 
      string rememberMeValue = (string)appRoamingSettings.Values["RememberMe"]; 
 

 
      if (rememberMeValue == "Yes") 
 
      { 
 
       //First check if the creds were saved 
 
       //If saved, check if the cookie is still valid (not past 2 days) 
 
       //If still valid, get the cookie value to pass it in an object to the MC page 
 
       //If not valid, using the saved creds, go back and get a new cookie and then pass that to the MC page 
 
       //Redirect to the MC page with the cookie in the object 
 

 
       //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 
 
       if (cookieTimeElapsedMins <= 2879) 
 
       { 
 
        //send the cookie to the MC page along with the cookie as an object 
 
        var shdCookie = myCookieValue; 
 
        var shdPageName = pagename; 
 
        // Create an object by populating the class with the data obtained from logging in and getting he cookie.... 
 
        myCookiePageName myNeededSHDData = new myCookiePageName(shdCookie, shdPageName); 
 

 
        //Pass the object as a paramter to the Naviage method since we need to pass the parameters to the page being navigated to.... 
 
        this.Frame.Navigate(typeof(MessageCenter), myNeededSHDData); 
 
       } 
 

 
       else 
 
       { 
 
        //get a new cookie 
 
        //send the cookie to the MC page along with the cookie as an object 
 

 
        //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; 
 
         var shdPageName = pagename; 
 

 
         // Create an object by populating the class with the data obtained from logging in and getting he cookie.... 
 
         myCookiePageName myNeededLoginData = new myCookiePageName(shdCookie, shdPageName); 
 

 
         //Pass the object as a paramter to the Naviage method since we need to pass the parameters to the page being navigated to.... 
 
         this.Frame.Navigate(typeof(MessageCenter), myNeededLoginData); 
 

 
         // Stop showing the progress bar...  
 
         mycontrols.progressbarNoShow(pgbar, pgText); 
 

 
        } 
 

 
        else 
 
        { 
 
         // Stop showing the progress bar... 
 
         mycontrols.progressbarNoShow(pgbar, pgText); 
 

 
         //Show the error message... 
 
         ServerNetworkError.Visibility = Windows.UI.Xaml.Visibility.Visible; 
 
        } 
 

 
       } 
 

 
      } 
 

 
      else 
 
      { 
 
       //If NOT saved, then redirect to the SignIn page along with the page name.. 
 

 
       var shdCookie = "currentCookieValue"; //Putting some default value.... 
 
       var shdPageName = pagename; 
 
       // Create an object by populating the class with the data obtained from logging in and getting he cookie.... 
 
       myCookiePageName myNeededSHDData = new myCookiePageName(shdCookie, shdPageName); 
 

 
       //Instantiate the frames class for using in this function since this.Frame.Navigate can't be used... 
 
       //Frame myframe = new Frame(); 
 

 
       //Frame Frame = new Frame(); 
 

 
       //Pass the object as a paramter to the Naviage method since we need to pass the parameters to the page being navigated to.... 
 
       this.Frame.Navigate(typeof(SHDSignIn), myNeededSHDData); 
 

 
      } 
 

 

 

 
     } 
 

我的問題是,每次我的代碼打「this.Frame」行就告訴我,框架爲空。我想我想看看如何引用相同的框架,我所有的應用程序引用頁面,所以我不會得到這個null?還是我需要其他的東西來引用正確的框架?

感謝

+1

我想我在那裏看到你的誤解:每個**頁**都有一個* Frame *屬性,它保存顯示頁面的Frame-Instance。您不能只創建一個新的Frame實例並使用它的導航方法(因爲該框架沒有顯示任何內容)。如果你想在助手類中使用它,你必須將原始實例傳遞到那裏。希望我不會對此有所瞭解,但是:看起來您應該閱讀一些關於OOP中的「對象,類和實例」的基礎知識。 – 2015-02-23 09:17:01

回答

0

的框架應該是你的應用程序,因爲它mantains應用程序已導航頁面內的共享實例(您的應用程序的堆棧中,確定按背部按鈕時,你看到的網頁如果有的話),從而重新創建一個新的不管用。

通常在Application類(大多數新項目嚮導應用程序中的「應用程序」類)中對其進行初始化,並設置爲Window.Current.Content。然後,同樣的底層實例也會被在應用程序中實例化的任何Page(由框導航)作爲Page中的Frame屬性公開。

所以,你可以從任何地方通過Window.Current.Content引用它,然後將其轉換爲Frame或最好是你作爲一個參數傳遞的東西到你的類(來自Page.Frame,一個應用廣泛的定義,它返回Window.Current.Content鑄造到Frame,或其他類的單例實例封裝了Frame,並且可用的應用程序範圍)需要它通過類構造函數或需要它的特定類方法。

+0

好吧,我看了你的回覆,也從MSDN論壇得到了一些幫助,以獲得答案...解決方案參考應用程序框架,然後使用....我在我的課堂中使用了以下內容:'框架rootFrame = Window.Current.Content as Frame; rootFrame.Navigate(typeof(Object),Object);'這解決了這個問題。感謝您的幫助。 – mujno 2015-02-23 23:10:22

+0

如果您同意這是您聽起來像是的答案,請將我的回答標記爲有用答案。 – 2015-02-24 16:47:31

相關問題