在 class 中为 windows phone 8.1 创建方法时出现框架问题
frame issue when creating a method in a class for windows phone 8.1
我正在尝试为 windows phone 8.1 通用运行时应用程序创建一个可以在我的代码中的多个页面上重复使用的方法。所以我将其中一个页面中的这个片段复制到我的助手 class 中。但是,我正在为 "Frame" class 苦苦挣扎。
我还最初在我的方法之外定义了 "Frame Frame;",以解决我最初复制代码时看到的问题。
这是方法片段:
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" 行时,它都会告诉我 Frame 为空。我想我正在尝试查看如何引用与我在应用程序中的所有页面引用相同的框架,这样我就不会得到这个空值?或者我需要在这里引用其他内容来引用正确的框架吗?
谢谢
Frame 应该是您的应用程序中的一个共享实例,因为它保留了应用程序导航的页面(您的应用程序的后台堆栈决定了您在按下后退按钮时看到的页面),因此重新创建一个新页面将不起作用。
通常这在您的 Application
class 中初始化("App" class 在大多数新项目向导应用程序中)并设置为 Window.Current.Content
。然后,相同的底层实例也被应用程序中实例化的任何 Page
公开(由框架导航到)作为 Page
.[=21= 中的 Frame
属性 ]
因此您可以通过 Window.Current.Content
从任何地方引用它,然后将其转换为 Frame
或者最好将它作为参数传递给 class(来自 Page.Frame
,一个 returns Window.Current.Content
强制转换为 Frame
的应用范围定义,或者封装 Frame
并且在应用范围内可用的另一个 class 单例实例)通过 class 构造函数或需要它的特定 class 方法需要它。
我正在尝试为 windows phone 8.1 通用运行时应用程序创建一个可以在我的代码中的多个页面上重复使用的方法。所以我将其中一个页面中的这个片段复制到我的助手 class 中。但是,我正在为 "Frame" class 苦苦挣扎。 我还最初在我的方法之外定义了 "Frame Frame;",以解决我最初复制代码时看到的问题。
这是方法片段:
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" 行时,它都会告诉我 Frame 为空。我想我正在尝试查看如何引用与我在应用程序中的所有页面引用相同的框架,这样我就不会得到这个空值?或者我需要在这里引用其他内容来引用正确的框架吗?
谢谢
Frame 应该是您的应用程序中的一个共享实例,因为它保留了应用程序导航的页面(您的应用程序的后台堆栈决定了您在按下后退按钮时看到的页面),因此重新创建一个新页面将不起作用。
通常这在您的 Application
class 中初始化("App" class 在大多数新项目向导应用程序中)并设置为 Window.Current.Content
。然后,相同的底层实例也被应用程序中实例化的任何 Page
公开(由框架导航到)作为 Page
.[=21= 中的 Frame
属性 ]
因此您可以通过 Window.Current.Content
从任何地方引用它,然后将其转换为 Frame
或者最好将它作为参数传递给 class(来自 Page.Frame
,一个 returns Window.Current.Content
强制转换为 Frame
的应用范围定义,或者封装 Frame
并且在应用范围内可用的另一个 class 单例实例)通过 class 构造函数或需要它的特定 class 方法需要它。