WindowsPhone项目组织结构&简单登陆例子(下)

移动开发
这里主要是软件生命周期中事件实现,已经保存全局变量或者数据等(比如:登陆用户账号,密码等),同时也包括了软件顶级容器:PhoneApplicationFrame。以上可以看出App主要是保存或者是定义全局的地方,你写程序的时候可以考虑需要保存全局的。

上一篇WindowsPhone项目组织结构&简单登陆例子(上)中 已经介绍了WP7项目组织结构,那么现在就让我们来进行实际开发吧,本来打算写一个helloworld的,但是这未免太对不起观众了,于是就改成做个登 陆的例子,当然这个登陆例子我们暂时不连接远程服务,就在文件中写死吧,以后讲到远程服务的时候必然会使用到的,这个登陆例子也可以作为后续开发使用。

一:新建一个Window phone application项目。

       因为我们是需要做登陆,那么必定是有用户账号,密码的,那么就建立一个类UerInfo.cs   ,添加属性

        public String userName;
        public String passworld;

二:我们的登陆是要访问服务端进行验证的,但是呢,我们现在还不需要服务器端,当然,我们可以模拟后台服务器端登陆验证:

      1:我们写一个接口,定义用户模块的一些方法,这里有一个登陆方法, UsetInfo Login(string userName,string password);

      2:定义个类,实现该接口的方法,比如上面登陆方法:

  1. public UsetInfo Login(string userName, string password)  
  2.         { 
  3.             UsetInfo info = null
  4.             if (userName.Equals("sa") && password.Equals("123456")) 
  5.             { 
  6.                 info=new UsetInfo(); 
  7.                 info.userName="sa"
  8.                 info.passworld="123456"
  9.             } 
  10.             return info; 
  11.         } 

三:我们模拟的服务器端数据写好后,就开始实现我们的UI了,UI比较简单,

2个TextBlock 控件(户名,密码显示),

1个TextBox 用来提供输入用户名,然后1个密码框:PasswordBox,用来接收用户输入的密码,设置属性passwordChar接收密码隐藏为:*

1个CheckBox 用来提供用户选择是否记住密码,注册Checked事件

1个Button控件,用来进行登陆提交,注册Click事件

当然我们可以提供一个进度条,ProgressBar ,可以注册ValueChanged事件,就是值改变事件,用来显示进度,这里我们暂时不用 。

然后拖动控件进行简单布局,如下:

http://images.51cto.com/files/uploadimg/20130410/1056250.jpg

四:现在就进入.cs文件中处理事件,接收户名,密码,然后调用登陆。当然如果用户勾选了“记住密码”,就需要保存户名,密码到本 机,下次打开软件时显示出来,那么怎么保存呢?这里我们用IsolatedStorageSettings(独立存贮,类似于键值对形式保存数据)

具体代码如下:

  1. //我们把用户对象保存进去 
  2.                        IsolatedStorageSettings.ApplicationSettings["UserInfo"] = usetInfo; 
  3.                        IsolatedStorageSettings.ApplicationSettings.Save(); 

当然开始加载页面时候也应该取出保存的UserInfo,并把户号,密码等设置在文本框中:

  1. //判断是否有键 
  2. if(IsolatedStorageSettings.ApplicationSettings.Contains("UserInfo")) 
  3.     UsetInfo usetInfo = IsolatedStorageSettings.ApplicationSettings["UserInfo"as UsetInfo; 
  4.     //显示在文本框中 
  5.     txtUserName.Text = usetInfo.userName; 
  6.     txtPassword.Password= usetInfo.passworld; 

五:很多时候我们登陆用户的一些信息需要保存起来提供给全局使用,那么必定要涉及保存全局的变量,上一篇文章中,我们知道App.xaml.cs里面可以保存全局性的东西,那么我们就把用户信息保存在App.xaml.cs里面吧,以便下次使用。

  1. //保存登陆用户(全局),App.xaml.cs 
  2.         private UsetInfo usetInfo; 
  3.         public UsetInfo GetUsetInfo()  
  4.         { 
  5.           return usetInfo; 
  6.         } 
  7.  
  8.         public void SetUsetInfo(UsetInfo usetInfo)  
  9.         { 
  10.             this.usetInfo = usetInfo; 
  11.         } 

在Main.xmal.cs中保存到全局中:

  1. //保存用户到全局变量中 
  2. App app= Application.Current as App; 
  3. if(app!=null
  4.     app.SetUsetInfo(usetInfo); 
  5.  
  6.     if (app.GetUsetInfo()!=null
  7.     MessageBox.Show("您已经登陆成功!,您已经保存对象到全局"); 

http://images.51cto.com/files/uploadimg/20130410/1056251.jpg

具体的还是看代码吧,如下:

  1. public partial class App : Application 
  2.     { 
  3.         /// <summary> 
  4.         /// Provides easy access to the root frame of the Phone Application. 
  5.         /// </summary> 
  6.         /// <returns>The root frame of the Phone Application.</returns> 
  7.         public PhoneApplicationFrame RootFrame { getprivate set; } 
  8.  
  9.         //保存登陆用户(全局) 
  10.         private UsetInfo usetInfo; 
  11.         public UsetInfo GetUsetInfo()  
  12.         { 
  13.           return usetInfo; 
  14.         } 
  15.  
  16.         public void SetUsetInfo(UsetInfo usetInfo)  
  17.         { 
  18.             this.usetInfo = usetInfo; 
  19.         } 
  20.  
  21.   bool? isChecked = false
  22.         // Constructor 
  23.         public MainPage() 
  24.         { 
  25.             InitializeComponent(); 
  26.             //注册事件 
  27.             initEventListener(); 
  28.         } 
  29.  
  30.         private void initEventListener()  
  31.         { 
  32.             this.Loaded += new RoutedEventHandler(MainPage_Loaded); 
  33.             btnLogin.Click += new RoutedEventHandler(btnLogin_Click); 
  34.             chkRecord.Checked += new RoutedEventHandler(chkRecord_Checked); 
  35.             progressBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(progressBar_ValueChanged); 
  36.         } 
  37.  
  38.         //本页加载时候根据独立存贮保存的内容,显示在文本框里 
  39.         void MainPage_Loaded(object sender, RoutedEventArgs e) 
  40.         { 
  41.             //判断是否有键 
  42.             if(IsolatedStorageSettings.ApplicationSettings.Contains("UserInfo")) 
  43.             { 
  44.                 UsetInfo usetInfo = IsolatedStorageSettings.ApplicationSettings["UserInfo"as UsetInfo; 
  45.                 //显示在文本框中 
  46.                 txtUserName.Text = usetInfo.userName; 
  47.                 txtPassword.Password= usetInfo.passworld; 
  48.             } 
  49.               
  50.         } 
  51.  
  52.         void progressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 
  53.         { 
  54.              
  55.         } 
  56.  
  57.         void chkRecord_Checked(object sender, RoutedEventArgs e) 
  58.         { 
  59.             if (sender!=null
  60.             { 
  61.                 CheckBox chkRecord = sender as CheckBox; 
  62.                 isChecked=chkRecord.IsChecked; 
  63.                 if (isChecked==true
  64.                 { 
  65.                     //判断是否被选中,然后保存到文件中或是独立存贮中,在下次启动时候就读取文件或独立存贮的内容 
  66.                     isChecked = true
  67.                 } 
  68.             } 
  69.         } 
  70.  
  71.         void btnLogin_Click(object sender, RoutedEventArgs e) 
  72.         { 
  73.             string userName = txtUserName.Text.Trim(); 
  74.             string password = txtPassword.Password.Trim(); 
  75.  
  76.             //调用服务器端进行数据验证登陆 
  77.              UsetInfo usetInfo= PhoneAppService.getInstance().getUserInfoService().Login(userName, password); 
  78.              if (usetInfo!=null
  79.             { 
  80.                 //保存用户到全局变量中 
  81.                 App app= Application.Current as App; 
  82.                 if(app!=null
  83.                 { 
  84.                     app.SetUsetInfo(usetInfo); 
  85.  
  86.                     if (app.GetUsetInfo()!=null
  87.                         MessageBox.Show("您已经登陆成功!,您已经保存对象到全局"); 
  88.                        
  89.                     //根据单选框选中情况保存数据到独立存贮中 
  90.                     if(isChecked==true
  91.                     { 
  92.                         //我们把用户对象保存进去 
  93. IsolatedStorageSettings.ApplicationSettings["UserInfo"] = usetInfo;                 IsolatedStorageSettings.ApplicationSettings.Save(); 
  94.                     } 
  95.                 } 
  96.             } 
  97.         } 

 

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-10 10:40:41

2009-07-16 15:14:27

WebWork用户登陆

2010-05-28 15:08:09

MySQL远程登陆

2009-07-24 15:46:00

ASP.NET登陆控件

2009-06-17 12:59:32

Linux

2009-06-16 09:38:39

Linux

2014-09-29 09:31:35

Angular

2023-12-05 07:26:21

Golang项目结构

2011-07-20 09:27:37

Scala

2010-06-07 10:44:01

MySQL远程登陆

2011-03-08 13:52:25

Proftpd

2010-04-21 17:20:03

Unix远程

2009-12-22 13:50:00

2009-11-10 17:31:38

VB.NET注册表

2012-02-14 10:46:15

WP Marketpl杂志月刊

2010-08-18 08:21:49

Adobe AIRAndroid

2009-12-15 17:28:58

戴尔互联课堂

2009-02-18 22:19:24

AD用户登陆实现限制

2012-11-07 10:09:11

组件技术OAuth授权登陆

2011-02-25 17:07:25

点赞
收藏

51CTO技术栈公众号