Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer...

33
Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science

Transcript of Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer...

Page 1: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Mobile Development

Introduction to Visual Studio Development

Rob MilesDepartment of Computer Science

Page 2: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Introduction

The “Secret Encoder” Program

> Encodes text entered, based on a simple keyword

Will allow us to explore the issues of mobile development

> User Input> Output Display> Use of the Menu Buttons> Program termination

Page 3: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Encoder Program

Screen contains three areas:> Encryption key entry> Secret message entry> Output display entry

When the code button is pressed the key is applied to the message to produce the result

The encryption is performed using XOR, so that it is reversible

Key: mykeytext

In: password

Out: xwbqegg231

Code

Page 4: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Starting Development

Initially I am going to write the program for a Smartphone

Then we will convert the code over and build a Pocket PC version

You will see that the conversion is not difficult and centres around the user interface

Finally we can use the encoder in a workspace that targets both devices

Page 5: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Encoder Project

First we have to add the data entry components for the program

The Visual Studio Toolbox holds only the components which are available for the Smartphone display

We are going to use Label and TextBox components

Page 6: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Encoder Form

I have added four Label and two TextBox components onto the form

They have been given sensible names and aligned correctly

Page 7: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Test Layout

When the program runs the layout is somewhat different

This is a limitation of Visual Studio 2003

Visual Studio 2005 gives a full preview

Page 8: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Field Traversal

The Smartphone application begins running with the most recently added TextBox selected first

Moving “forwards” through the fields actually moves towards the “oldest” TextBox

This is not what the user will want You should add the lowest TextBox first

> Or make use of the tool to change the field order

Page 9: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Smartphone Tab Order

The Smartphone Tab order dialogue, selected from the View menu allows you to re-arrange the tab order of the fields on the form

Page 10: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Correct Tabs

The order on the right is the one required

Note that although the labels are not selected by the tab operation, they appear on the order dialogue

Page 11: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Data Entry

The present program will run, and the user can move between the two entry boxes and type text into either

We now need a way of triggering the encode action

Page 12: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Adding Menu Keys

Not all forms may have menu keys

You can edit them by selecting the MainMenu item at the bottom of the form designer

Page 13: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Menu Keys

Once you have added the keys to the form you can type menu selections onto them by clicking on the “Type Here” item

Page 14: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Adding the Encode Selection

The Encode selection can be added by typing the option name over the button

Note that I have the option to type above the selection to create multi-option menus

Page 15: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Smartphone Menu

The menu key is displayed as shown

We now need to bind an event to the key press to perform our encode action

Page 16: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Binding to Menu Actions

It is very easy to bind to a key event In the form designer, double click on the menu item If there is no event hander for that item, one is created

and you are taken to it in the source file

Page 17: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Encode Action

I have created a method to perform the encode action I could have placed this code inside the event hander

itself, but it is more flexible to create a method to do the job:private void doEncode ()

{

outputTextLabel.Text = encode ( inTextBox.Text, keyTextBox.Text ) ;

}

This calls an encode method which actually performs the translation of the text itself

Page 18: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Testing the program

The initial version of the encode method simply returns the original text

This allows me to test the program and ensure that it works correctly

Page 19: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

The Encode method

private string encode ( string input, string key )

{

char [] keyChars = key.ToCharArray();

int keypos = 0 ;

System.Text.StringBuilder result = new System.Text.StringBuilder();

foreach ( char ch in input )

{

int cval = ch - 32;

int pval = keyChars[keypos] - 32;

result.Append((char)((cval ^ pval) + 32));

keypos++;

if ( keypos == key.Length ) keypos = 0 ;

}

return result.ToString() ;

}

Page 20: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Secret Encoder

The encryption works, but it is a very weak method

Note that the space in the input reveals the key character at that position

I leave it to you to create a better one!

Page 21: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Exiting the program

At the moment there is no way to exit the program on the Smartphone device

We can stop it with Visual Studio, but the user will not be able to do that

It is very easy to add an exit menu option

Page 22: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Stopping an Application

We can use the standard application termination method to stop the program:private void menuItem2_Click(object sender, System.EventArgs e)

{Application.Exit();

}

This frees off any resources and exits the program cleanly

Note that the Windows CE guidelines aren’t keen on you stopping your programs (but I do it anyway)

Page 23: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

TextBox Key Entry

You may wish to not show the password when you are using the program

As with standard Windows Forms, the TextBox component can be set to allow password entry

Page 24: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Debugging

The program can be debugged in exactly the same manner as any other Visual Studio application

Any exceptions which are thrown are trapped and you are given the option to debug

Page 25: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Running on the PC

The application will run on a standard PC

Note how the behaviour of the menu has been adapted for a Windows Form

It is often useful to be able to run programs on a PC to test them

> Particularly if they make use of file input/output

Page 26: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Labels and Bugs

There is a bug in this program; some characters are interpreted by the label display component as controls for access keys (even though these have no meaning for Samrtphone!)

The key of “cheese” gives the output as shown

Page 27: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

TextBox Replacement

Using a TextBox set to read only allows the correct text to be displayed

The read only property of the text box is used to prevent the user changing it

Page 28: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Pocket PC Version

The Pocket PC version is very similar

Just about all of the Smartphone code can be transferred directly over to Pocket PC

The only issue is that of user input using buttons

Page 29: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Sample Project 01Smartphone Encoder

If you run the program you will find that you can enter and encode text using a key that you supply

If the encoded text is entered it is converted back into the original

Page 30: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Sample Project 02Pocket PC Encoder

The Pocket PC version is virtually identical to the Smartphone one

The only change is the use of a button to receive the encode command

Page 31: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Developing the Application

You may wish to further develop this application

Suggested enhancements include> Improved encoding method> Separate encode/decode methods (perhaps using public and

private keys)

Page 32: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

Sample Project 03

This project combines a Smartphone and a Pocket PC version of the encoder in a single Visual Studio workspace

They all share the same encoder behaviour

Note the references to the Encoder resource

Page 33: Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.

More Mobile Fun

If you want to make use of remote resources you should take a look at Web Services

A sample web service is based at MoreThanOneCanPlay.com

Details are on the delegate CD