• WP7开发第一课:WP7项目组织结构&简单登陆例子(其二)


    源地址:http://www.cnblogs.com/zhangdongzi/archive/2011/08/26/2155173.html

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

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

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

            public String userName;
            public String passworld;

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

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

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

    View Code
    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;
    }

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

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

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

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

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

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

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

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

    具体代码如下:

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

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

                //判断是否有键
                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("您已经登陆成功!,您已经保存对象到全局");

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

    View Code
    publicpartialclass 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; privateset; }

    //保存登陆用户(全局)
    private UsetInfo usetInfo;
    public UsetInfo GetUsetInfo()
    {
    return usetInfo;
    }

    publicvoid SetUsetInfo(UsetInfo usetInfo)
    {
    this.usetInfo = usetInfo;
    }
    View Code
    bool? isChecked =false;
    // Constructor
    public MainPage()
    {
    InitializeComponent();
    //注册事件
    initEventListener();
    }

    privatevoid 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);
    }

    //本页加载时候根据独立存贮保存的内容,显示在文本框里
    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)
    {
    //判断是否被选中,然后保存到文件中或是独立存贮中,在下次启动时候就读取文件或独立存贮的内容
    isChecked =true;
    }
    }
    }

    void btnLogin_Click(object sender, RoutedEventArgs e)
    {
    string userName = txtUserName.Text.Trim();
    string password = txtPassword.Password.Trim();

    //调用服务器端进行数据验证登陆
    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(
    "您已经登陆成功!,您已经保存对象到全局");

    //根据单选框选中情况保存数据到独立存贮中
    if(isChecked==true)
    {
    //我们把用户对象保存进去
    IsolatedStorageSettings.ApplicationSettings["UserInfo"] = usetInfo;
    IsolatedStorageSettings.ApplicationSettings.Save();
    }
    }
    }
    }

    六:就到这里,接下来下一篇应该是说一些基本的控件用法了。

  • 相关阅读:
    STL(1) 指针迭代器
    不错我博主
    C++ 学习书目
    写给VC++ Windows开发的初学者 一片不错的博文
    算法:C语言实现 (4)队列的数组实现
    算法:C语言实现 (4)下推栈的数组实现
    算法:C语言实现 (4)下推栈的链表实现
    vs 使用笔记
    自定义组合控件,适配器原理-Day31
    Android30-Fragment-理解
  • 原文地址:https://www.cnblogs.com/linlf03/p/2246763.html
Copyright © 2020-2023  润新知