2014-03-28 42 views
1
string uname = txt1.Text; 
string pwd = txt2.Text; 
NavigationService.Navigate(new Uri("/newPage.xaml?name="+uname+"&pwd="+pwd,UriKind.Relative)); 

我有兩個文本框:usernamepassword 這些textboxes現在我輸入值,並將這些值例如:如何在Windows手機創建會話

username: abcd 
password:1234 

現在我想這些值在多個頁面中,如何可能? 我正在使用查詢字符串,但每次我必須定義值navigation URI, 所以,請給我建議像SESSION在ASP.NET中的任何其他方式。

+0

你可以使用靜態變量來保存值的權利? –

+0

我該如何使用它?我是Windows手機應用程序中的新手,大約1個月。所以,你能告訴我怎麼做嗎? –

+0

@VirajShah PhoneApplicationService.Current.State是另一種工作方式。 – Jaihind

回答

2

一旦登錄是成功的,可以複製的用戶名和密碼爲靜態具有相同的命名空間的應用程序變量以便它可以在每個頁面中訪問。

public static string Username; 
public static string Password; 

希望這將解決您的問題

+0

謝謝你的哥們! –

2

創建您設置的靜態公共變量。例如:

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

然後你可以簡單的設置值的任何地方:

AppState.Username = "Viraj"; 
+0

感謝您的回覆兄弟,但我的用戶名和密碼將每次不同。請參閱我編輯的代碼。 –

+0

感謝brother.it的作品! –

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"); 
       } 
     } 
+0

+1 - 使用PhoneApplicationService.Current.State時,數據將在應用程序爲Tombstoned時保留。 – Romasz

0
IsolatedStorageSettings.ApplicationSettings["uname"] = uname; 

然後調用它在其他頁面,如:

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

這也適用更大。