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