Tải bản đầy đủ - 0 (trang)
Chapter 2.  What Is a .NET Compact Framework Program?

Chapter 2.  What Is a .NET Compact Framework Program?

Tải bản đầy đủ - 0trang

Chapter2.WhatIsa.NETCompact

FrameworkProgram?

Inthelastchapterwelookedatthe.NETCompact

Frameworkfromahigh-level,architectural

perspective.Thischapteranswersthequestion"What

isa.NETCompactFrameworkapplication?"by

studyingthedetailsofasimplebutcomplete.NET

CompactFrameworkWindowsapplication.Alongthe

way,weintroduceelementsthatarecommontoevery

.NETCompactFrameworkprogram,includingforms,

controls,classes,events,andeventhandlers.Wealso

explaintherelationshipbetweenthecodegenerated

foryoubytheVisualStudio.NET2003WindowsForms

Designerandthecodeyoumustwriteyourself.

Amazon



Prev



don'tbeafraidofbuyingbooks



Next



TheEssenceofa.NETCompactFramework

Program

A.NETCompactFrameworkWindowsprogramisa

programbuilttousethe.NETCompactFramework

libraries.AsdiscussedinChapter1,the.NETCompact

FrameworklibrariesrunontheWin32APIsupport

providedbytheunderlyingWindowsCEoperatingsystem.

ThesesameWin32APIservicescanbedirectlyaccessed

froma.NETCompactFrameworkprogram,usingthe

P/Invokeservicesofthe.NETCompactFramework(see

Chapter4fordetailsonP/Invoke).

A.NETCompactFrameworkprogramcansupportanyof

severaluserinterfaceparadigms..NETCompact

Frameworkprogramscanrunheadless—asaninvisible

process—withnouserinterface,asaconsoleapplication,[1]

orasaWindowsapplication.Anyofthesemightdrawon

oneormoreclasslibrariesthatyoudevelop.Whilenot

denyingtheimportanceofheadlessandconsole-based

userinterfaces,thefocusofthisbookisonbuilding

Windowsapplications—whichmeansapplicationswitha

GUI.Forthepurposesofthisbook,then,wedefinea.NET

CompactFrameworkprogramasaprogramwiththe

followingelements:

[1]Character-basedapplicationsrelyonsupportfromtheunderlyingWindowsCEoperating

system.Someplatforms—mostnotablythePocketPCfamilyofplatforms—donotsupporta

character-baseduserinterface.



Isbuilttousethe.NETCompactFrameworklibraries

HasaGUI

Isdefinedentirelyinaccessiblesourcecode



Hasoneormoreclassesderivedfromthe

System.Windows.Forms.Formclass,oneofwhichis

instantiatedatapplicationstartupastheapplication's

mainwindow

Letustakealookateachelementofthisdefinition.



Usesthe.NETCompactFrameworkLibraries

AswediscussedinChapter1,the.NETCompact

FrameworklibrariesareasetofDLLs.Asmallpartof

theseareWin32libraries,builtwithnative,unmanaged

code.Mostofthe.NETCompactFrameworklibrariesare

.NETmanagedlibraries,andlikemanagedapplications

thesemanagedlibrariesareportablebetweendifferent

CPUarchitectures.

.NETCompactFrameworkprogramsrequirethepresence

ofthe.NETCompactFrameworklibraries,whichcanbe

eitherbuiltintodeviceROMorinstalledintotheRAMbasedfilesystem.Havingthe.NETCompactFrameworkin

ROMisrequiredforPocketPC2003devices.Forearlier

PocketPCs,the.NETCompactFrameworkhastobe

installedintotheRAMfilesystem.(VisualStudio.NET

2003createscabinetfiles,withtheextension.cab,toset

up.NETCompactFrameworkapplications,butthese.cab

filesdonotincludethe.NETCompactFrameworklibraries.

Aseparatesetof.cabfilescontainingtheruntimelibraries

isavailablewiththe.NET2003development

environment.)



HasaGraphicalUserInterface

A.NETCompactFrameworkWindowsapplicationconsists



offormsandcontrolscontainedwithinthoseformsthat

executeinanevent-drivenGUIenvironment.The

applicationpresentsinformationtotheuser,andtheuser

respondsbyinteractingwiththeformsandcontrols

throughtheuseofeithermouseandkeyboardorstylus,

touchscreen,andtheon-screenSoftwareInputPanelon

supporteddevices.

Aneventisapredefinednotificationfromanobjectthat

indicatessomethinghashappenedthatmightbeof

interesttootherobjects(e.g.,textFirstName'stexthas

changed,orbuttoncmdConnectToDatabasehasbeen

tapped,orconnSQLCConnection'sconnectionhas

successfullybeenopened).Aswewillseelaterinthis

chapter,anyotherobjectthatholdsareferencetothe

sourceobjectcansignuptoreceiveevents)fromthe

sourceobject.Whateverthesourceofanevent,the

destinationisaneventhandler.Eventhandlersareclass

methodsoftherecipient.Theirsolejobistowatchforand

respondtoevents.

Ifyouhaveworkedinanyevent-basedprogramming

environment,youwillfindthat.NETCompactFramework

eventhandlershavemanyfamiliarelements.Whenthe

paradigmofevent-drivenGUIprogrammingwasfirst

introducedintheearly1980s—firstontheAppleMacintosh

in1984andtheninMicrosoftWindows1.01in1985—it

seemedarcaneanddifficulttocomprehend.Sincethen,it

hasbecomethemainstreammodelforbuildinginteractive

userinterfaces.ThebasicmodelforallGUIsisthesame;

thedifferencesareprimarilyinthesyntaxoffunctioncalls

andinthedetailsofthesupportedfeatures.



IsDefinedEntirelyinSourceCode

A.NETCompactFrameworkapplicationconsistsentirelyof



code.WhenyoudrawformsandcontrolsintheVisual

StudioDesigner,itdoesnotcreatedatadefinitionsofthese

elementsbutinsteadgeneratescode.Socodedefines

formsandcontrols,codesetsthepropertiesofformsand

controls,andcodehandlesevents.Thisisgoodnewsto

programmers,whobythenatureoftheirworkareusedto

reading,writing,and—fromtimetotime—debuggingcode.

Defininguserinterfaceobjectsentirelyincodeisdifferent

fromthewayenvironmentslikeVisualBasic6operate.

WithVisualBasic6,whenadeveloperdefinescontrols,

theyaregeneratedbythedevelopmentenvironmentand

hiddenfromtheprogrammer.Itisalsodifferentfromwhat

Win32andMFCprogrammersareusedto,wheredialogs

andmenusaredrawningraphicaleditorsbutthenstored

awayinthestaleandoftenobscureformatofresource

(.rc)files.Eventhoughyoucanviewandeveneditthese

textdefinitions,theyaredeaddata,andtheyrelyon

hiddenoperatingsystemcodetobringthemtolife.

Inthe.NETCompactFrameworkenvironment,every

changeyoumakeintheDesignercausescodetobe

generatedautomaticallyforyou.Thiscodeisnothidden

fromyou;youcanexamineitanduseittolearnallkinds

ofinterestingthingsabout.NETCompactFramework

classes.Toacertainextent,youcanalsomodifythiscode.



ModifyingDesigner-GeneratedCode

AlthoughyoucanmodifyDesigner-generatedcode,

insomecasessuchchangeswillbelostwhenthe

Designerregeneratesthecode.Ifyouintroduce

errorsintothecode,youmayevenbreakthe

Designer.



Thuseverythingina.NETCompactFrameworkapplication

isdefinedwithinitssourcecode,whichhasleadtotheoftquotedline,"Youcanwritea.NETapplicationinNotepad."

However,almostnoonedoesthisbecausetheVisual

Studio.NETdevelopmentenvironmentisthemuchbetter

toolforproducing.NETCompactFrameworkWindows

applications.



HasOneorMoreClassesDerivedfrom

System.Windows.Forms.Form

A.NETCompactFrameworkapplicationconsistsofoneor

moreforms,eachofwhichiscreatedfromaclassderived

fromtheSystem.Windows.Forms.Formbaseclass.

BecauseeachformisaclassthatderivesfromtheForm

baseclass,eachformconsistsofPMEs.ThePMEs

supportedbySystem.Windows.Forms.FormarethePMEs

thatexistwithintheformsthatyourapplicationcreates.

.NETCompactFrameworkformshaveaBackColor

propertybecauseSystem.Windows.Forms.Formhasa

BackColorproperty..NETCompactFrameworkformsdo



nothaveanOpacityproperty,however,becausethat

propertydoesnotexistinSystem.Windows.Forms.Form.A

completelistofsupportedPMEsforthe

System.Windows.Forms.FormclassappearsinChapter5.

Oneoftheseformsistheapplication'smainwindow.When

theapplicationstartsrunning,thismainwindowappears

totheuser.Itisthemaindoorthroughwhichallthe

featuresoftheprogramaremadeavailable.

Ourdefinitioniscomplete.It'stimetoentertheVisual

Studio.NETapplicationdevelopmentenvironmentand

createa.NETCompactFrameworkWindowsapplication.

Amazon



Prev



don'tbeafraidofbuyingbooks



Next



UsingVisualStudio.NET2003

Tocreateanew.NETCompactFrameworkapplication,

startVisualStudio.NET2003andselecttheFile New

Project…menuitem.The.NETdevelopmentenvironment

respondswiththeNewProjectdialogbox(Figure2.1).



Figure2.1.TheNewProjectdialogbox

[Viewfullsizeimage]



AllsamplesinthisbookareinC#,soselectVisualC#

ProjectsintheProjectTypepane.ThenselectSmart



DeviceApplicationintheTemplatespane,andspecifythe

applicationnameanddirectory.Whenreadytoproceed,

clicktheOKbutton.

Thenextstepincreatinga.NETCompactFramework

applicationistospecifythetargetplatformandtheproject

typetocreate.YoucandothisintheSmartDevice

ApplicationWizard(Figure2.2),whichappearsafteryou

clickOKintheNewProjectdialogbox.(Fordetailson

WindowsCEplatforms,seethediscussioninChapter1.

Note:Theappearanceofthewizardmayvaryfrom

platformtoplatform.)



Figure2.2.TheSmartDeviceApplication

Wizard

[Viewfullsizeimage]



Therearefivepossibleprojecttypesthatyoucancreatein

.NETCompactFramework;fourofwhichareavailablefor

PocketPC,andfourforWindowsCE,assummarizedin

Table2.1



Table2.1.ProjectTypesinthe.NETCompact

Framework



ProjectType



Description



WindowsApplication



AGUIapplicationthat

usesformsandcontrolsto

interactwiththeuser



ClassLibrary



Amanaged-codeDLL



Non-graphicalApplication Anapplicationwithno

userinterface;not

availableonthePocketPC

ConsoleApplication



Anapplicationwitha

character-baseduser

interface;notavailableon

thePocketPC



EmptyProject



Aprojectfilewithno

sourcefiles



InresponsetoarequesttocreateaWindowsApplication

forthePocketPC,theSmartDeviceApplicationWizard

generatesallthefilesneededtosupporta.NETCompact

Frameworkapplicationandstoresthosefilesinthe

directoryspecifiedearlier.VisualStudio.NETnextpasses

controltotheDesigner(Figure2.3).



Figure2.3.TheVisualStudio.NET2003

Designer

[Viewfullsizeimage]



Younowhaveablankcanvasonwhichyoucanbegin

shapingtheelementsofyourmasterpiece—a.NET



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 2.  What Is a .NET Compact Framework Program?

Tải bản đầy đủ ngay(0 tr)

×