Lesson 1 of WP7 development: WP7 project organization structure & simple login example (II)

Source: Internet
Author: User

 

The organizational structure of the WP7 project has been introduced in the previous section. Now let's perform actual development. I was going to write a helloworld project, but I'm sorry for the audience, so I changed it to a login example. Of course, if we do not connect to the remote service for the time being, we will write it in the file. In the future, we will inevitably use the remote service, this login example can also be used for subsequent development.

1. Create a window phone application project.

Because we need to log on, there must be a user account and password, so we will create a class uerinfo. CS and add attributes.

Public String username;
Public String passworld;

2. Our login is to access the server for verification. However, we do not need the server now. Of course, we can simulate the login verification on the backend server:

1: Let's write an interface to define some methods of the user module. Here we have a login method, usetinfo login (string username, string password );

2: Define a class to implement the interface method, such as the above login method:

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

3: after we have written the simulated server data, we began to implement our UI, which is relatively simple,

Two textblock controls (account name and password display ),

One textbox is used to provide the input user name, and then one password box: passwordbox, which is used to receive the password entered by the user. Set the passwordchar receiving password to be hidden :*

One checkbox is used to enable the user to select whether to remember the password and register the checked event.

One button control for login submission and registration of click events

Of course, we can provide a progress bar, which can be used to register a valuechanged event, that is, a value change event to display the progress. We do not need it here.

Drag the control to make a simple layout, as shown below:

4. Now go to The. CS file to process the event, receive the account name and password, and then call the logon method. Of course, if you select "Remember password", you need to save the account name and password to the local machine. The next time you open the software, how can you save it? Here we use isolatedstoragesettings (independent storage, similar to key-value pairs for data storage)

DetailsCodeAs follows:

// Save the user object
Isolatedstoragesettings. applicationsettings ["userinfo"] = usetinfo;
Isolatedstoragesettings. applicationsettings. Save ();

Of course, when loading the page, you should also retrieve the saved userinfo and set the account number and password in the text box:

// determines whether a key exists.
If (isolatedstoragesettings. applicationsettings. contains ("userinfo")
{< br> usetinfo = isolatedstorageset.pdf. applicationsettings ["userinfo"] As usetinfo;
// displayed in the text box
txtusername. TEXT = usetinfo. username;
txtpassword. password = usetinfo. passworld;
}

V. In many cases, some information about login users must be saved for global use. Therefore, global variables must be saved.Article.

// Save the login user (global), app. XAML. CS
Private usetinfo;
Public usetinfo getusetinfo ()
{
Return usetinfo;
}

Public void setusetinfo (usetinfo)
{
This. usetinfo = usetinfo;
}

Save it to the global storage in Main. Xmal. CS:

// Save the user to a global variable
App = application. Current as app;
If (app! = NULL)
{
App. setusetinfo (usetinfo );

If (App. getusetinfo ()! = NULL)
MessageBox. Show ("You have logged in successfully !, You have saved the object to the global ");

Check the Code as follows:

View code

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

// Save logon user (global)
Private Usetinfo;
Public Usetinfo getusetinfo ()
{
Return Usetinfo;
}

Public Void Setusetinfo (usetinfo)
{
This . Usetinfo = Usetinfo;
}

View code

   Bool  ?  Ischecked  =     False  ;
// Constructor
Public Mainpage ()
{
Initializecomponent ();
// Register an event
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 );
}

// When this page is loaded, it is displayed in the text box according to the content stored separately.
Void Mainpage_loaded ( Object Sender, routedeventargs E)
{
// Determines whether a key exists.
If (Isolatedstoragesettings. applicationsettings. Contains ( " Userinfo " ))
{
Usetinfo = Isolatedstoragesettings. applicationsettings [ " Userinfo " ] As Usetinfo;
// Display in text box
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 )
{
// Check whether the file is selected, save it to the file or store it independently, and read the file or the content stored independently at the next startup.
Ischecked = True ;
}
}
}

Void Btnlogin_click ( Object Sender, routedeventargs E)
{
String Username = Txtusername. Text. Trim ();
String Password = Txtpassword. Password. Trim ();

// Call the server for data verification and login
Usetinfo = Phoneappservice. getinstance (). getuserinfoservice (). login (username, password );
If (Usetinfo ! = Null )
{
// Save the user to a global variable
App = Application. Current As APP;
If (App ! = Null )
{
App. setusetinfo (usetinfo );

If (App. getusetinfo () ! = Null )
MessageBox. Show ( " You have logged in successfully !, You have saved the object to global " );

// Save data to independent storage based on the selected region
If (Ischecked = True )
{
// We save the user object
Isolatedstoragesettings. applicationsettings [ " Userinfo " ] = Usetinfo;
Isolatedstoragesettings. applicationsettings. Save ();
}
}
}
}

6: Now, the next article will introduce some basic control usage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.