Ch02_What Is a Compact Framework.pdf

(261 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Chapter 2
What Is a Compact Framework
Program?
The last chapter looked at the Compact Framework from a high-level, architectural
perspective. This chapter answers the question “What is a .NET Compact Framework
application?” in two ways: by studying the details of a simple, but complete, Compact
Framework Windows application. Along the way,we introduce elements that are common to
every Compact Framework program, including forms, controls, object classes, events, and
event handlers. We also explain the relationship between the code generated for you by the
Visual Studio .NET Forms Designer and the code that you must write yourself. [Comment 2_cs.1]
Step 2: Adding Code to the Cistern program .......................................................................................... 24
Adding a new form to an application.................................................................................................... 24
Understanding Events and Event Handlers.........................................................................................26
Creating and Displaying a Second Form ............................................................................................. 27
Adding a Class ............................................................................................................................................ 29
Creating the WaterMath Classes ............................................................................................................ 31
Function Overloading .............................................................................................................................. 33
Using the WaterMath Object ................................................................................................................... 34
Defining and Using Static Methods ......................................................................................................... 34
Namespaces............................................................................................................................................ 36
Using IntelliSense.................................................................................................................................... 39
Conclusion .................................................................................................................................................. 42
Chapter 2 – What is a Compact Framework Program?
Page 1
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
What is a Compact Framework program?
A Compact Framework Windows program is a program built to use the .NET Compact
Framework libraries. As discussed in chapter 1, the Compact Framework libraries run on the
Win32 support provided by the underlying Windows CE operating system. These same Win32
services can be directly accessed from a Compact Framework program, using the Platform
Invoke (a.k.a. “P/Invoke”) services of the Compact Framework (see chapter 4 for details on
P/Invoke). [Comment 2_cs.2]
A Compact Framework program can support any of several user-interface paradigms. CF
programs can run headless – as an invisible process – with no user-interface, with a character-
based user-interface 1 , or as a Windows application. While not denying that importance of
headless and character-based user interfaces, the focus of this book is on building Windows
applications – which means applications with a Graphical User Interface (GUI). For the
purposes of this book, then, we define a Compact Framework program as a program with the
following elements: [Comment 2_cs.3]
Built to use the Compact Framework libraries
Has a Graphical User Interface
Is defined entirely in accessible source code
Has one or more object classes derived from the System.Windows.Forms.Form class, one of
which is instantiated at application startup as the application’s main window.
Let us take a look at each element of this definition. [Comment 2_cs.4]
Built to use the Compact Framework libraries
As we discuss in chapter 1, the Compact Framework libraries are a set of dynamic link
libraries. A small part of these are Win32 libraries, built with native, unmanaged code. Most of
the Compact Framework libraries are .NET managed libraries, and like managed applications
these managed libraries are portable between different CPU architectures. [Comment 2_cs.5]
Compact Framework programs require the presence of the Compact Framework libraries,
which can either be built into device ROM, or installed into the RAM-based file system. The
Pocket PC 2003 is expected to ship with the Compact Framework in ROM. For earlier Pocket
PCs, the Compact Framework has to be installed into the RAM file system. (Visual Studio .NET
2003 creates CAB files to set up Compact Framework applications; these CAB files also include
the Compact Framework libraries.) [Comment 2_cs.6]
Has a Graphical User Interface
A Compact Framework Windows application consists of forms, and controls contained within
those forms, executing in an event-driven GUI environment. The application presents
information to the user, to which the user responds by interacting with the forms and controls
using mouse – on a Pocket PC using stylus to a touch screen – and keyboard – on a Pocket PC
using the on-screen Software Input Panel (SIP). Each response appears to the application in
1 Character-based support relies on that support in the underlying Windows CE operating system. Some
platforms – most notably the Pocket PC family of platforms – do not support a character-based user-
interface.
Chapter 2 – What is a Compact Framework Program?
Page 2
Copyright © 2003 Paul Yao & David Durant
870585293.010.png 870585293.011.png 870585293.012.png
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
the form of events . In other cases, events are generated without direct user action, such as
when a timer expires. Whatever the source of an event, the destination is an Event Handler .
Event handlers are class methods whose sole job is to watch for and respond to events.
If you have worked in any event-based programming environment, you will find that
Compact Framework event handlers have many familiar elements. When the paradigm of
event-driven GUI programming was first introduced in the early 1980s – first on the Apple
Macintosh in 1984 and then in Microsoft Windows 1.01 in 1985 – it seemed arcane and difficult
to comprehend. Since then, it has become the mainstream model for building interactive user-
interfaces. The basic model for all GUIs is the same, the differences are primarily in the syntax
of function calls, and in details of the supported features. [Comment 2_cs.8]
Defined Entirely in Source Code
A Compact Framework application consists entirely of code. When you draw forms and
controls in the Visual Studio Forms Designer, it does not create data definitions of these
elements – but instead generates code. So code defines forms and controls, code sets the
properties of forms and controls, and code handles events. This is good news to programmers,
who by the nature of their work are used to reading, writing, and – from time to time –
debugging code. [Comment 2_cs.9]
The definition of user-interface objects entirely in code is different from the way
environments like Visual Basic 6 operate. With VB6, when a developer defines controls, they
are generated by the development environment and hidden from the programmer. It is also
different from what Win32 and MFC programmers are used to, where dialogs and menus are
drawn in graphical editors, but then stored away in the stale and often obscure format of
resource (.rc) files. Even though you can view and even edit these text definitions, they are
dead data which rely on hidden operating system code to bring them to life. [Comment 2_cs.10]
In the Compact Framework environment, every change you make in the Forms Designer
causes code to be generated automatically for you. This code is not hidden from you, but
instead you can examine it, and use it to learn all kinds of interesting things about Compact
Framework classes. It is visible to you and – to a certain extent – modifiable by you, the
programmer. (While you are able to modify Forms Designer-generated code, in some cases
such changes will be lost when the Forms Designer regenerates that code. If you introduce
errors into that code, you may even break the Forms Designer.) [Comment 2_cs.11]
And so everything in a Compact Framework application is defined within its source code,
which has lead to the oft-quoted line that “You can write a .NET application in Notepad”.
Everybody quotes this but nobody does it, as the Visual Studio .NET development environment
is the much better tool for producing Compact Framework Windows applications. [Comment
Has one or more object classes derived from System.Windows.Forms.Form
A Compact Framework application consists of one or more forms, each of which is created
from a class derived from the System.Window.Forms.Form base class. Since each form is an
object class that derives from the Form base class, it consists of properties, methods, and
events – or PMEs as the Compact Framework team calls them (a term we use throughout this
book). The PMEs supported by System.Windows.Forms.Form are the PMEs that exist within
the forms that your application creates. Compact Framework forms have a BackColor property
because System.Windows.Forms.Form has a BackColor property. Compact Framework forms
do not have an Opacity property, however, because that property does not exist in
Chapter 2 – What is a Compact Framework Program?
Page 3
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
System.Windows.Forms.Form . A complete list of supported PMEs for the
System.Windows.Forms.Form class appears in chapter 5, Creating Forms . [Comment 2_cs.13]
One of these forms is the application’s main window. When the application starts running,
this main window appears to the user. It is the main door through which all the features of the
program are made available. [Comment 2_cs.14]
Our definition is complete. It's time to enter the Visual Studio .NET application development
environment and create a Compact Framework Windows application. [Comment 2_cs.15]
Using Visual Studio .NET 2003
To create a new Compact Framework application, we start Visual Studio .NET 2003 running
and select File->New->Project… menu item. The .NET development environment responds with
the New Project dialog box shown in Figure 2-1. [Comment 2_cs.16]
Figure 2-1 - The New Project Dialog Box [Comment 2_cs.17]
All samples in this book are in C#, so we select Visual C# Projects in the project type pane.
We then select Smart Device Application in the templates pane, and specify the application name
and directory. When ready to proceed, click the Ok button. [Comment 2_cs.18]
The next step in creating a Compact Framework application is to specify the target platform
and the project type to create. We do this within the Smart Device Application Wizard , which
appears next as shown in Figure 2-2. (For details on Windows CE platforms, see the discussion
in chapter 1) [Comment 2_cs.19]
Figure 2-2 – The Smart Device Application Wizard [Comment 2_cs.20]
Chapter 2 – What is a Compact Framework Program?
Page 4
Copyright © 2003 Paul Yao & David Durant
870585293.013.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
There are five available project types of projects that we could create, as summarized in this
Project Type
Description
Windows Application
GUI application that uses forms and controls to interact with the user.
Class Library
A managed-code dynamic link library (DLL)
Non-Graphical Application
On the Pocket PC, an application with no user-interface
Console Application
An application with character-based user-interface (not available on
Pocket PC)
Empty Project
Project file with no source files
In response to our request to create a Windows Application for the Pocket PC, the Smart Device
Application Wizard generates all the files needed to support a .NET Compact Framework
application and stores those files in the directory specified earlier. Visual Studio next passes
control to the Forms Designer, as shown in Figure 2-3. [Comment 2_cs.22]
Figure 2-3 – The Visual Studio .NET 2003 Forms Designer [Comment 2_cs.23]
Chapter 2 – What is a Compact Framework Program?
Page 5
Copyright © 2003 Paul Yao & David Durant
870585293.001.png 870585293.002.png 870585293.003.png 870585293.004.png 870585293.005.png 870585293.006.png 870585293.007.png 870585293.008.png 870585293.009.png
 
Zgłoś jeśli naruszono regulamin