Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 (2006)

.pdf
Скачиваний:
219
Добавлен:
17.08.2013
Размер:
14.97 Mб
Скачать

Chapter 22

allow you to start creating mobile applications with a small learning curve. Just like the parent Framework, the CF is based on the Common Language Runtime (CLR) and executes Microsoft Intermediate Language (MSIL) to maintain platform independence. The greatest difference you will see is the number of controls that are missing and the number of overloaded methods, properties, and constructors that are not present for controls. Application deployment is different also. Deploying applications to a PDA is not going

to be done via CD or floppy disk. You will use Microsoft ActiveSync to facilitate deploying applications to a Pocket PC. Next, we will elaborate on what you need to know before you create your first mobile application.

Common Language Runtime

One goal of the CLR, also known as the execution engine, is to allow multiple languages to run side by side. The CLR manages the following items:

Memory management or garbage collection

Platform independence via Microsoft Intermediate Language

Common type system

Exception handling

Just-In-Time compilation

ActiveSync

To connect your mobile device to your desktop, you will most likely use ActiveSync. This software will quickly get you connected to your PC, your network resources, and even a shared Internet connection. You can use the ActiveSync connection to synchronize your application data, access Web Services, or even replicate data to a SQL Server machine on your network. All you need is to download the software and connect your device to your desktop. When you are connected using ActiveSync, you will have options such as those shown in Figure 22-1 and Figure 22-2. The first figure is the interface for the ActiveSync program. Figure 22-2 shows the use of Windows Explorer on the desktop to browse a Pocket PC.

Figure 22-1

706

Building Mobile Applications

Figure 22-2

Since most of you do not have Pocket PCs, I will briefly explain the ease of deploying a smart device application to a Pocket PC. To deploy a smart device application, Visual Studio 2005 includes a smart device .cab file template under setup and deployment projects. For deploying, add the .cab file project to your device application project. When you build the project, just copy the output to the device using ActiveSync. Next, double click on the .cab file using the device, and it will install the application. That is all it takes to deploy your applications.

You can download the latest version of Microsoft ActiveSync from Microsoft. The .exe for ActiveSync version 3.7.1, used here, was a 3.77MB download. Use the Search for ActiveSync at the Microsoft download site. http://www.microsoft.com/downloads/.

Common in the Compact Framework

The type system available in the Compact Framework should look familiar to you when you start to program mobile applications. Table 22-1 lists the types available in the CF. As you allocate memory with variables in your applications, remember this chapter and the fact that you are working with very limited resources. Refer here for a quick reference to make sure you use the smallest data type possible to store data.

Table 22-1: Common Types

VB type

CLR type

Size

Value range

 

 

 

 

Boolean

Boolean

Depends

True or False

Byte

Byte

1 byte

0 through 255 (unsigned)

Char

Char

2 bytes

Single unicode character

Date

DateTime

8 bytes

0:00:00 (midnight) on January 1, 0001, through

 

 

 

11:59:59 PM on December 31, 9999

 

 

 

Table continued on following page

707

Chapter 22

Table 22-1: Common Types (continued)

VB type

CLR type

Size

Value range

 

 

 

 

Decimal

Decimal

16 bytes

0 through +/-

 

 

 

79,228,162,514,264,337,593,543,950,335 (+/-

 

 

 

7.9...E+28) with no decimal point; 0 through

 

 

 

+/-7.9228162514264337593543950335 with 28

 

 

 

places to the right of the decimal;smallest nonzero

 

 

 

number is +/-0.0000000000000000000000000001

 

 

 

(+/-1E-28)

Double

Double

8 bytes

-1.79769313486231570E+308 through -

 

 

 

4.94065645841246544E-324 for negative val-

 

 

 

ues;4.94065645841246544E-324 through

 

 

 

1.79769313486231570E+308 for positive values

Integer

Int32

4 bytes

-2,147,483,648 through 2,147,483,647 (signed)

Long

Int64

8 bytes

-9,223,372,036,854,775,808 through

 

 

 

9,223,372,036,854,775,807 (9.2...E+18) (signed)

Object

Object

4 bytes

Any type can be stored in a variable of type

 

 

 

Object

SByte

SByte

1 byte

-128 through 127 (signed)

Short

Int16

2 bytes

-32,768 through 32,767 (signed)

Single

Single

4 bytes

-3.4028235E+38 through -1.401298E-45 for nega-

 

 

 

tive values;1.401298E-45 through 3.4028235E+38

 

 

 

for positive values

String

String

Depends

0 to approximately 2 billion Unicode characters

UInteger

UInt32

4 bytes

0 through 4,294,967,295 (unsigned)

ULong

UInt64

8 bytes

0 through 18,446,744,073,709,551,615 (1.8...E+19)

 

 

 

(unsigned)

UShort

UInt16

2 bytes

0 through 65,535 (unsigned)

 

 

 

 

The Compact Framework Classes

The .NET Compact Framework classes are a subset of the .NET Framework classes plus a few inclusions that you do not need for desktop applications. Overall, you will not be surprised by the controls you find in the CF.

The .NET Compact Framework is approximately 12 percent of the size of the full framework. That makes it easy to see that, although many of the common controls and namespaces are included, the functionality has been greatly reduced. You will notice quickly that controls like the Label control are missing properties. Take a look at Figure 22-3 to see the differences between label properties available in both frameworks.

708

Building Mobile Applications

Compact Framework Label

.NET Framework Label

Figure 22-3

Two other missing pieces are method overrides and events. Figure 22-4 compares the events of the Button control in both frameworks. The events listed from the full framework on the right of Figure 22-4 are just a partial list and represent about half of the available events.

709

Chapter 22

Compact Framework Button

.NET Framework Button

Figure 22-4

How many overloaded methods have been removed? Based on the size of the CF, you can estimate that more than 75 percent are missing. When you look at the System.IO.FileStream constructors, you will see that the full Framework boasts 14 overloaded constructors, while the CF has been whittled down to only five. When you write applications that will be used on smart devices and desktops, you will have to adjust certain parts of your code to address these differences.

A partial list of the default controls available to a Pocket PC application is shown in Table 22-2.

Table 22-2: Default Controls in the .NET Compact Framework

Name

Name

Button

NumericUpDown

CheckBox

OpenFileDialog

ComboBox

Panel

ContextMenu

PictureBox

DataConnector

ProgressBar

DataGrid

RadioButton

DateTimePicker

RichInk

710

Building Mobile Applications

Name

Name

 

 

DocumentList

SaveFileDialog

DocumentList

SaveFileDialog

DomainUpDown

Splitter

HScrollBar

StatusBar

ImageList

TabControl

InputPanel

TextBox

Label

Timer

LinkLabel

ToolBar

ListBox

TrackBar

ListView

TreeView

MainMenu

VScrollBar

MonthCalendar

WebBrowser

Notification

 

 

 

Building a Pocket PC Game

For your first mobile application, you will build a simple game of tic-tac-toe. You will see how different the screen is and design a user interface that is simple for the user.

Try It Out:

Tic Tac Toe

1.In Visual Studio 2005, select the File New Project menu. This will display the New Project dialog box.

2.Select Visual Basic from the Project Types pane on the left. Next, expand the Smart Device label and then select Pocket PC 2003. In the templates pane on the right, choose Pocket PC 2003 Application. Change the project name to PocketPC_TicTacToe and click the OK button. The dialog is shown in Figure 22-5.

3.The project opens to a view of a Pocket PC, as shown in Figure 22-6. This is the design environment. As you build the application, you will see the screen and be able to design the graphical interface on a replica of the actual device screen. You will know exactly how the application will look when the user installs it.

711

Chapter 22

Figure 22-5

Figure 22-6

712

Building Mobile Applications

4.Now, you will build the user interface. Add ten buttons to the form as shown in Figure 22-7. The three rows of three buttons represent the tic-tac-toe board. Set the Size to 40, 40 for the 9 buttons that make up the board. Starting with the button in the upper left of the board, move left to right, down a row, left to right, down a row, and then left to right again, and name the buttons on the board btn00, btn01, btn02, btn10, btn11, btn12, btn20, btn21, btn22. The name begins with btn followed by the row (0, 1 or 2) and column (0, 1 or 2) of the button. So btn02 is on the first row (Row 0) and the third column (Column 2). When you use these names in code, you will know the location on the board. Next, set the Font for all of the board buttons to Tahoma, 24pt, Bold. Finally, set the Text property to X and the Anchor property to None for the board buttons. The final button is the New Game button. Set the Name to btnNewGame and the Text to &New Game. Below the board, add a label named lblMessages. Make the label tall enough to display two lines of text. Now, change the Text property of Form1 to TicTacToe, and the user interface is complete.

Figure 22-7

713

Chapter 22

5.Switch to the code behind view and add the following highlighted code to the Form1 class:

Public Class Form1

‘Get the game ready to start again Sub ResetGame()

Dim ctrl As Control

‘Loop through the board controls and set them to “” For Each ctrl In Me.Controls

If TypeOf (ctrl) Is Button And ctrl.Name <> “btnNewGame” Then ctrl.Text = String.Empty

End If Next

lblMessages.Text = “” ‘Enable the board buttons CorrectEnabledState(True)

End Sub

Private Sub CorrectEnabledState(ByVal ButtonEnabledState As Boolean)

Dim ctrl As Control

For Each ctrl In Me.Controls

If TypeOf (ctrl) Is Button And ctrl.Name <> “btnNewGame” Then

ctrl.Enabled = ButtonEnabledState

End If

Next

End Sub

Private Sub CorrectEnabledState()

Dim ctrl As Control

For Each ctrl In Me.Controls

If TypeOf (ctrl) Is Button And ctrl.Name <> “btnNewGame” Then

If ctrl.Text = String.Empty Then

ctrl.Enabled = True

Else

ctrl.Enabled = False

End If

End If

Next

End Sub

Sub ComputerPlay()

Dim RandomGenerator As New Random() Dim intRandom As Integer

Dim intCount As Integer = 0 Dim ctrl As Control

intRandom = RandomGenerator.Next(20, 100) While intCount < intRandom

For Each ctrl In Me.Controls

If TypeOf (ctrl) Is Button And ctrl.Name <> “btnNewGame” Then If ctrl.Text = String.Empty Then

intCount += 1

If intCount = intRandom Then ctrl.Text = “O”

714

Building Mobile Applications

ctrl.Enabled = False Exit For

End If End If End If

Next

End While End Sub

Private Sub TicTacToe_Click(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles btn00.Click, btn20.Click, btn10.Click, _ btn01.Click, btn21.Click, btn11.Click, btn02.Click, btn22.Click, btn12.Click

CorrectEnabledState(False)

Application.DoEvents() ‘Allows the screen to refresh CType(sender, Button).Text = “X”

If IsGameOver() Then MsgBox(“Game Over”)

Else

lblMessages.Text = “Computer selecting ...” Application.DoEvents() ‘Allows the screen to refresh ComputerPlay()

If IsGameOver() Then MsgBox(“Game Over”)

Else

lblMessages.Text = “Select your next position ...” CorrectEnabledState()

End If End If

End Sub

Sub Winner(ByVal strWinner As String) Dim strMessage As String

If strWinner = “X” Then strMessage = “You win!!”

ElseIf strWinner = “O” Then strMessage = “Computer wins!!”

Else

strMessage = strWinner End If

lblMessages.Text = strMessage End Sub

Function IsGameOver() As Boolean

If btn00.Text = btn01.Text And btn01.Text = btn02.Text And _

btn02.Text <> String.Empty Then

‘Winner on top Row

Call Winner(btn00.Text)

Return True

End If

If btn10.Text = btn11.Text And btn11.Text = btn12.Text And _ btn12.Text <> String.Empty Then

‘Winner on middle Row Call Winner(btn10.Text)

715