如何在Windows Phone中创建SESSION

时间:2014-03-28 05:57:58

标签: session c#-4.0 windows-phone-8

string uname = txt1.Text;
string pwd = txt2.Text;
NavigationService.Navigate(new Uri("/newPage.xaml?name="+uname+"&pwd="+pwd,UriKind.Relative));

我有两个文本框:usernamepassword 现在我在那些textboxes上输入值,例如:

username: abcd
password:1234

现在我想在多个页面中使用这些值,那么它怎么可能呢? 我正在使用查询字符串,但每次我必须使用navigation URI定义值, 所以请建议我使用ASP.NET中的SESSION。

4 个答案:

答案 0 :(得分:3)

public class Users
 {
    public string Username { get; set; }
    public string Password { get; set; }
 }

  Users objUser = new Users();
  objUser.Username = "Viraj";
  objUser.Password = "12345";

//save data in phone state use in multiple pages.
      PhoneApplicationService.Current.State["UserInfo"] = objUser;

//To retrieve data on another screen from phone state

 if(PhoneApplicationService.Current.State["UserInfo"]!=null)
     {
       Users objUser = PhoneApplicationService.Current.State["UserInfo"] as Users;
     } 

//To update data in phone state

  if(PhoneApplicationService.Current.State["UserInfo"]!=null)
    {
       Users objUser = PhoneApplicationService.Current.State["UserInfo"] as Users;
       objUser.Username = "aman";
       PhoneApplicationService.Current.State["UserInfo"] = objUser;
     }

//at last remember that always remove data from phone state on app exist

private void Application_Closing(object sender, ClosingEventArgs e)
        {
          if(PhoneApplicationService.Current.State["UserInfo"]!=null)
                 {
                   PhoneApplicationService.Current.State.Remove("UserInfo");
                 }
        }

答案 1 :(得分:2)

创建您设置的静态公共变量。例如:

public static class AppState
{
    public static string Username { get; set; }
    public static string Password { get; set; }
}

然后您可以在以下任意位置设置值:

AppState.Username = "Viraj";

答案 2 :(得分:2)

登录成功后,您可以将用户名和密码复制到与应用程序具有相同名称空间的静态变量,以便在每个页面中都可以访问它。

public static string Username;
public static string Password;

希望这能解决您的问题

答案 3 :(得分:0)

IsolatedStorageSettings.ApplicationSettings["uname"] = uname;

然后在其他页面中调用它:

string name = IsolatedStorageSettings.ApplicationSettings["uname"] as string;

这也更有效。