WindowsPhone項目組織結(jié)構(gòu)&簡單登陸例子(下)
上一篇WindowsPhone項目組織結(jié)構(gòu)&簡單登陸例子(上)中 已經(jīng)介紹了WP7項目組織結(jié)構(gòu),那么現(xiàn)在就讓我們來進行實際開發(fā)吧,本來打算寫一個helloworld的,但是這未免太對不起觀眾了,于是就改成做個登 陸的例子,當(dāng)然這個登陸例子我們暫時不連接遠程服務(wù),就在文件中寫死吧,以后講到遠程服務(wù)的時候必然會使用到的,這個登陸例子也可以作為后續(xù)開發(fā)使用。
一:新建一個Window phone application項目。
因為我們是需要做登陸,那么必定是有用戶賬號,密碼的,那么就建立一個類UerInfo.cs ,添加屬性
public String userName;
public String passworld;
二:我們的登陸是要訪問服務(wù)端進行驗證的,但是呢,我們現(xiàn)在還不需要服務(wù)器端,當(dāng)然,我們可以模擬后臺服務(wù)器端登陸驗證:
1:我們寫一個接口,定義用戶模塊的一些方法,這里有一個登陸方法, UsetInfo Login(string userName,string password);
2:定義個類,實現(xiàn)該接口的方法,比如上面登陸方法:
- public UsetInfo Login(string userName, string password)
- {
- UsetInfo info = null;
- if (userName.Equals("sa") && password.Equals("123456"))
- {
- info=new UsetInfo();
- info.userName="sa";
- info.passworld="123456";
- }
- return info;
- }
三:我們模擬的服務(wù)器端數(shù)據(jù)寫好后,就開始實現(xiàn)我們的UI了,UI比較簡單,
2個TextBlock 控件(戶名,密碼顯示),
1個TextBox 用來提供輸入用戶名,然后1個密碼框:PasswordBox,用來接收用戶輸入的密碼,設(shè)置屬性passwordChar接收密碼隱藏為:*
1個CheckBox 用來提供用戶選擇是否記住密碼,注冊Checked事件
1個Button控件,用來進行登陸提交,注冊Click事件
當(dāng)然我們可以提供一個進度條,ProgressBar ,可以注冊ValueChanged事件,就是值改變事件,用來顯示進度,這里我們暫時不用 。
然后拖動控件進行簡單布局,如下:
四:現(xiàn)在就進入.cs文件中處理事件,接收戶名,密碼,然后調(diào)用登陸。當(dāng)然如果用戶勾選了“記住密碼”,就需要保存戶名,密碼到本 機,下次打開軟件時顯示出來,那么怎么保存呢?這里我們用IsolatedStorageSettings(獨立存貯,類似于鍵值對形式保存數(shù)據(jù))
具體代碼如下:
- //我們把用戶對象保存進去
- IsolatedStorageSettings.ApplicationSettings["UserInfo"] = usetInfo;
- IsolatedStorageSettings.ApplicationSettings.Save();
當(dāng)然開始加載頁面時候也應(yīng)該取出保存的UserInfo,并把戶號,密碼等設(shè)置在文本框中:
- //判斷是否有鍵
- if(IsolatedStorageSettings.ApplicationSettings.Contains("UserInfo"))
- {
- UsetInfo usetInfo = IsolatedStorageSettings.ApplicationSettings["UserInfo"] as UsetInfo;
- //顯示在文本框中
- txtUserName.Text = usetInfo.userName;
- txtPassword.Password= usetInfo.passworld;
- }
五:很多時候我們登陸用戶的一些信息需要保存起來提供給全局使用,那么必定要涉及保存全局的變量,上一篇文章中,我們知道App.xaml.cs里面可以保存全局性的東西,那么我們就把用戶信息保存在App.xaml.cs里面吧,以便下次使用。
- //保存登陸用戶(全局),App.xaml.cs
- private UsetInfo usetInfo;
- public UsetInfo GetUsetInfo()
- {
- return usetInfo;
- }
- public void SetUsetInfo(UsetInfo usetInfo)
- {
- this.usetInfo = usetInfo;
- }
在Main.xmal.cs中保存到全局中:
- //保存用戶到全局變量中
- App app= Application.Current as App;
- if(app!=null)
- {
- app.SetUsetInfo(usetInfo);
- if (app.GetUsetInfo()!=null)
- MessageBox.Show("您已經(jīng)登陸成功!,您已經(jīng)保存對象到全局");
具體的還是看代碼吧,如下:
- public partial class App : Application
- {
- /// <summary>
- /// Provides easy access to the root frame of the Phone Application.
- /// </summary>
- /// <returns>The root frame of the Phone Application.</returns>
- public PhoneApplicationFrame RootFrame { get; private set; }
- //保存登陸用戶(全局)
- private UsetInfo usetInfo;
- public UsetInfo GetUsetInfo()
- {
- return usetInfo;
- }
- public void SetUsetInfo(UsetInfo usetInfo)
- {
- this.usetInfo = usetInfo;
- }
- bool? isChecked = false;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- //注冊事件
- initEventListener();
- }
- private void initEventListener()
- {
- this.Loaded += new RoutedEventHandler(MainPage_Loaded);
- btnLogin.Click += new RoutedEventHandler(btnLogin_Click);
- chkRecord.Checked += new RoutedEventHandler(chkRecord_Checked);
- progressBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(progressBar_ValueChanged);
- }
- //本頁加載時候根據(jù)獨立存貯保存的內(nèi)容,顯示在文本框里
- void MainPage_Loaded(object sender, RoutedEventArgs e)
- {
- //判斷是否有鍵
- if(IsolatedStorageSettings.ApplicationSettings.Contains("UserInfo"))
- {
- UsetInfo usetInfo = IsolatedStorageSettings.ApplicationSettings["UserInfo"] as UsetInfo;
- //顯示在文本框中
- txtUserName.Text = usetInfo.userName;
- txtPassword.Password= usetInfo.passworld;
- }
- }
- void progressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- }
- void chkRecord_Checked(object sender, RoutedEventArgs e)
- {
- if (sender!=null)
- {
- CheckBox chkRecord = sender as CheckBox;
- isChecked=chkRecord.IsChecked;
- if (isChecked==true)
- {
- //判斷是否被選中,然后保存到文件中或是獨立存貯中,在下次啟動時候就讀取文件或獨立存貯的內(nèi)容
- isChecked = true;
- }
- }
- }
- void btnLogin_Click(object sender, RoutedEventArgs e)
- {
- string userName = txtUserName.Text.Trim();
- string password = txtPassword.Password.Trim();
- //調(diào)用服務(wù)器端進行數(shù)據(jù)驗證登陸
- UsetInfo usetInfo= PhoneAppService.getInstance().getUserInfoService().Login(userName, password);
- if (usetInfo!=null)
- {
- //保存用戶到全局變量中
- App app= Application.Current as App;
- if(app!=null)
- {
- app.SetUsetInfo(usetInfo);
- if (app.GetUsetInfo()!=null)
- MessageBox.Show("您已經(jīng)登陸成功!,您已經(jīng)保存對象到全局");
- //根據(jù)單選框選中情況保存數(shù)據(jù)到獨立存貯中
- if(isChecked==true)
- {
- //我們把用戶對象保存進去
- IsolatedStorageSettings.ApplicationSettings["UserInfo"] = usetInfo; IsolatedStorageSettings.ApplicationSettings.Save();
- }
- }
- }
- }