don't be afraid of buying books Next .NET Compact Framework Programming with C# By Paul Yao, David Durant Publisher : Prentice Hall PTRPub Date : May 24, 2004ISBN : 0-321-17403-8Pages
Trang 1don't be afraid of buying books Next
.NET Compact Framework Programming with C#
By Paul Yao, David Durant
Publisher : Prentice Hall PTRPub Date : May 24, 2004ISBN : 0-321-17403-8Pages : 1424
.NET Compact Framework Programming with C# is the definitive tutorial and
reference for the NET Compact Framework(CF) It shows you how to transfer your skillsand your code to the Pocket PC 2003 andother mobile and embedded smart devices
Authors Paul Yao and David Durant draw upontheir years of research and experience withmembers of the Microsoft NET CF team toshow you exactly how the best CF
programming gets done in C# This is theonly book a programmer needs to master theart of CF coding Throughout the book,
sample code and examples illustrate bestpractices and programming techniques Inaddition, the companion Web site includesdownloadable code for all these examples,along with a set of development tools to helpyou with your CF development projects
The authors expertly zero in on whatprogrammers need to understand for
Trang 2• Table of
Contents
successful smart device programming,including:
Garbage Collection in the NET CompactFramework
Controls and supported Properties,Methods, and Events (PMEs)
Custom controls with Windows FormsDesigner support
Using Platform Invoke (P/Invoke)
Simple and complex data binding and theDataGrid control
Programming with ADO.NET data classes
Synchronizing SQL Server CE with SQLServer 2000 databases
Creating graphical output, including fontand printing coding tricks
Differences between the standard desktopframework and the CF
.NET Compact Framework Programming with C# is your single resource for
everything you really need to know about CFprogramming
Amazon
Trang 3Prev don't be afraid of buying books Next
By Paul Yao, David Durant
Publisher : Prentice Hall PTRPub Date : May 24, 2004ISBN : 0-321-17403-8Pages : 1424
Trang 8Prev don't be afraid of buying books Next
Trang 9Many of the designations used by manufacturers and
sellers to distinguish their products are claimed as
trademarks Where those designations appear in this book,and Addison-Wesley was aware of a trademark claim, thedesignations have been printed with initial capital letters or
in all capitals
The NET logo is either a registered trademark or
trademark of Microsoft Corporation in the United Statesand/or other countries and is used under license from
Microsoft
Microsoft, Windows, Visual Basic, Visual C#, and VisualC++ are either registered trademark or trademarks of
Microsoft Corporation in the U.S.A and/or other
countries/regions
The authors and publisher have taken care in the
preparation of this book, but make no expressed or impliedwarranty of any kind and assume no responsibility for
errors or omissions No liability is assumed for incidental orconsequential damages in connection with or arising out ofthe use of the information or programs contained herein
The publisher offers discounts on this book when ordered
in quantity for bulk purchases and special sales For moreinformation, please contact:
U.S Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact:
Trang 10For information on obtaining permission for use of materialfrom this work, please submit a written request to:
Pearson Education, Inc
Rights and Contracts Department
75 Arlington Street, Suite 300
Trang 11Amazon
Trang 12Prev don't be afraid of buying books Next
Trang 13Programming with C#
"For nearly two decades, Paul Yao and David Duranthave been acknowledged as experts on the Windowsplatform, so it's only natural that they would bring
their experienced point of view to the NET CompactFramework With a unique combination of historicalperspective and in-depth understanding of the subjectmatter, Yao and Durant take the reader through notonly the technical guts of the Compact Framework butalso the reasons behind the design decisions."
—Joshua Trupin, Executive Editor, MSDN Magazine
"Yao and Durant have written a book that, although itassumes no prior experience with the NET Framework,serves both the rookie and advanced programmer
equally well This is definitely a rare quality amongtechnical books and is certainly not an easy thing for
—Bill Draper, Director of Software Development
"This book serves as both a great reference and
tutorial when building NET Compact Framework
applications My only wish is that it had been availablesooner."
Trang 14Healthcare Solutions
"Of the handful of books on Compact Framework, thisbook takes the cake Paul Yao and David Durant's
expertise with NET Compact Framework is evidentfrom their excellent and very insightful coverage ofsections such as Building the User Interface, ManagingDevice Data, and Creating Graphical Output The
chapter discussing the topic of P/Invoke is
unparalleled After reviewing this book, I am certainthat if there is one book that will help you understand.NET Compact Framework, this is the one."
—Deepak Sharma, Senior Systems Specialist, Tata
Infotech Ltd.
"Yao and Durant's fresh, innovative, and in-depth look
at the NET Compact Framework gets developers up tospeed using C# to develop robust and scaleable
handheld software solutions A definite must-read formobile handheld developer enthusiasts!"
—Andrew Krowczyk, Software Architect, Zurich North
America
Amazon
Trang 15Prev don't be afraid of buying books Next
Trang 16Amazon
Trang 17Prev don't be afraid of buying books Next
Trang 19Prev don't be afraid of buying books Next
Trang 20Welcome, future smart-device developer! Today we
embark on an exciting journey For the first time, mobiledevices are impacting the lives of users in profound ways.Mobile devices have become an everyday ingredient in thelives of millions of people around the world, whether theyare mobile employees working from the field or casual
consumers playing the latest and greatest games For
example, my Pocket PC Phone Edition has more memoryand processing power than the high-end PC I bought for apremium not even five years ago I use my mobile devicefor everything from handling e-mail and browsing the Web
to playing games, music, and videos In many ways, it hasreplaced the heavy laptop I once lugged from meeting tomeeting
At the same time, the innovation in this space has driventhe need for custom software development With newerand more powerful devices comes the need for newer andmore powerful software development tools Driving thisphenomenon—as always—is you, the software developer
To assist you, Microsoft is providing Visual Studio NET andthe NET Compact Framework, the most capable
development and runtime environments available, to
enable you to make the most of opportunities with yourcustomers, including mobile employees, casual consumers,and even the most uncompromisingly rabid device
enthusiasts (our authors)
So why has mobile development become so important all
of a sudden? Well, as any veteran developer can tell you,mobile development has been around for a long time Infact, they often reminisce about the good old days of
developing with eMbedded Visual Basic (Hey, six months
Trang 21in the consumer and enterprise markets
The first and most obvious factor driving this phenomenon
is the introduction of Visual Studio NET 2003 and the NETCompact Framework Combined, these tools provide a
development powerhouse that enables you as a developer
to take advantage of programming languages and skillsyou already know and use for developing desktop
applications, including a rich subset of class libraries andthe same secure, managed environment You'll learn moreabout this in Part I, Foundations
The next major factor is the Microprocessor Effect (alsoknown as Moore's Law), which describes the regular
doubling of the number of circuits in microprocessors Atpresent, circuit doubling occurs about every 18 months Inreality, the adoption of more processing power for mobiledevices has exceeded the growth suggested by Moore'sLaw But it hasn't been only increases in processing powerthat have exceeded predictions—increases in the capacity
of other necessary components, such as memory and
peripheral capabilities, have occurred as well
The continued improvements in hardware capabilities
coupled with the continued decrease in hardware costshave created an environment that has allowed for the
proliferation of different types of mobile and embeddeddevices Today, the NET Compact Framework supportssoftware development on the various generations of thePocket PC, the Pocket PC Phone Edition, and the MicrosoftSmartphone It also provides great support for WindowsCE–powered smart devices that run Windows CE NET 4.1and later
Trang 22User Interface The growth in device capacity means thatmobile devices can have enough local storage to collectsignificant volumes of data, even when disconnected—asubject covered in this book in Part III, Managing DeviceData The added horsepower also supports your ability tobuild stunning graphics, a topic that Paul and Dave address
in Part IV, Creating Graphical Output
Let us not forget, the NET Compact Framework has nativesupport for consuming Web Services, the industry standardfor integrating applications across any platform Throughthe detailed and extensive samples and discussions in thisbook, you will soon be an expert in building the most
reliable, effective, data-driven, graphical applications;
employing part-time and full-time connectivity; and using
a variety of data storage solutions including SQL ServerWindows CE Edition as well as integration with virtuallyany data source on the server
Whether you are a seasoned Windows CE programmer, adeveloper familiar with using Visual Studio NET and the.NET Framework, or someone totally new to both mobiledevelopment and NET programming, this is the only bookyou will need Whatever your background, this book
provides the technical insights and coding samples youneed to transfer your existing skills to the NET CompactFramework
This is truly an exciting time to be part of the mobile
development industry I would like to be among the first towelcome and congratulate you on joining the community
of NET mobile developers
Trang 23Ed Kaim
Product Manager
Windows Developer PlatformMicrosoft Corporation
Amazon
Trang 24Prev don't be afraid of buying books Next
Trang 25We feel pretty fortunate During the past two decades, wehave traveled the world teaching programmers what weknow The two of us have led many classes and togetherhave taught thousands of smart, highly motivated softwareengineers, test engineers, and development managers Weenjoy working with the people who are inventing the
future, and we enjoy being at the cutting edge of each
successive wave of software development technology Wehave learned much from the discussions we have had withthese students This book represents one way we can
Things have changed a lot during the 17 years since ourfirst book came out A pocket-sized computer now has
more memory and CPU power than the typical desktopsystem of the 1980s A typical desktop system has moreraw computing power than a roomful of computers hadback then With this increase in capacity has come a
dramatically improved set of development tools available
to software developers: Online programming references,context-sensitive help, and graphical editors all help
support the task of software development Programmerscan be more productive today thanks to all these tools
Trang 26programming interfaces While the NET initiative provides
a new set of programming interfaces that are better
organized than any other Microsoft has created, it is stillvery large and very intricate Given enough time, mostprogrammers can master these intricacies But most
programmers do not have enough time to learn about anew technology while also building new software, yet that
articles, and in the talks we give at conferences We
concentrate our efforts on helping software engineers
become more productive Time saved in learning aboutsoftware development issues can be focused on meetingthe specific needs of the end users whom you are
supporting (or, perhaps, on taking some time off betweenprojects to recharge your mental and physical batteries)
Amazon
Trang 27Prev don't be afraid of buying books Next
What You Need to Use This Book
To make the most of what this book has to offer, you aregoing to need a few things, as described in the following
subsections
Hardware
Software developers have historically had the fastest andmost capable computer systems they could get their hands
technology is very good—more than good, in fact Today'semulators provide an excellent replication of the software
Trang 28drivers are not going to be the same on an actual device
as on an emulator running on a PC This is the major cause
of differences between an emulator and an actual device.When we teach our Pocket PC programming classes, wealways recommend to participants that a major portion oftesting be done on devices
[1] Historically, not all emulators have provided high fidelity to device software In
particular, Windows CE 1.x and 2.x emulators were based on an older technology that was good but not great The last emulator to use this older technology was the Pocket PC
emulator that shipped with Microsoft eMbedded Visual C++ 3.0 All emulators for Visual Studio NET 2003 use the newer emulation technology.
While the focus of this book is on writing code, the reality
of software development is that you spend a lot of timedebugging your code For that reason, you want to removeanything that slows down your debugging We suggest youinvest in a network connection between your developmentsystem and your smart-device system, which means youneed the following:
Network card (wired or wireless) for the desktop
development system
Network card (wired or wireless) for the Windows CE–powered device
Software
The development tools can run on any supported version
of Microsoft Windows However, an emulator requires thatyou run on a 32-bit version of the operating system Thismeans you want one of the following:
Microsoft Windows 2000 (with Service Pack 2 or later)Microsoft Windows XP Professional
Trang 29With the right operating system in place, you can then usethe software development tools The first item in the
following list is required; the other items are "nice-to-have" tools
Microsoft Visual Studio NET 2003 (required)
(Optional) Microsoft eMbedded Visual C++ 3.0 (forPocket PC and Pocket PC 2002)
Microsoft eMbedded Visual C++ 4.0 (for Pocket PC
2003, Smartphone 2003, and later)
P/Invoke Wizard, available from The Paul Yao
Company, for help in creating declarations needed tocall native code from the NET Compact Framework.(Learn more about this tool and download a demo
version from http://www.paulyao.com/pinvoke.)
The Latest Version of the NET Compact
Framework
As we were finishing this book, Microsoft made availableService Pack 2 of the NET Compact Framework Shouldyou upgrade to this latest version (or whatever later
version might be available by the time you read this)? Wemust say yes! This is a new technology, and the NET
Compact Framework team is still working on
improvements and upgrades to make the library work
better and faster After shipping a new library, the NETCompact Framework team members—like most
development team members at Microsoft—take a weekend
or two off, and then their reward for their hard work is thatthey get to start all over again, working to create
Trang 30something even better It's a good thing that they likewhat they are doing—and that they are so good at it.
Trang 31You can download the code for this book from thefollowing URL:
http://www.paulyao.com/cfbook/code
When you install the sample code directory treefrom the Web site, you see four top-level
location: \CS\Ch05_CreatingForms\
location: \VB\Ch05_CreatingForms\FormEvents
Trang 32Amazon
Trang 33Prev don't be afraid of buying books Next
.NET Compact Framework is still pretty good Chapter 1
Trang 34benefits for programmers who have worked with otherframeworks Getting involved with NET Compact
Framework programming is a great way to get into theworld of NET because the libraries are scaled back fromthe (sometimes) overwhelming number of classes and
Compact Framework today will help you learn to build Webapplications using ASP.NET, Windows Forms applicationsfor the desktop, and managed-code stored procedures forYukon (the code name for the next version of SQL Server)
If you have been using the core Windows API/Win32, thenyou might wonder whether to continue writing Win32 code
or to jump into the NET Compact Framework For somethings, including the following, you must continue usingWin32.[2]
[2] For a detailed discussion of these, see the following white paper on the Microsoft Web site: http://msdn.microsoft.com/library/en-us/dncenet/html/choose_api.asp
Fastest executables
Best real-time support
Source code portability between platforms
Ability to wrap Component Object Model (COM) foraccess by NET Compact Framework applications
Ability to create device drivers
Ability to create control panel applets
Trang 35Support for security extensions
Ability to build Simple Object Access Protocol (SOAP)Web Service servers
Support for Pocket PC shell extensions
Ability to use existing Win32 code
For Programmers Experienced with the NET Framework
If you are experienced with writing programs that use the.NET Framework, you are going to find much that is
familiar The C# and Visual Basic NET languages, for onething, use the same syntax for two very popular languagesthat are available for desktop NET development The
fundamental data types that support interoperability
between different languages on the desktop also play acore part of interoperability with smart-device programs
One thing that may be surprising to desktop NET
Framework developers is the extent to which they mightneed to rely on P/Invoke support to call the underlyingWin32 API functions that Windows CE supports While thedesktop NET Framework provides an extensive set of
Trang 36on the support available in the NET Compact Framework(or, perhaps, a limit on the support of the underlying
Windows CE operating system) The attitude you take indealing with such experiences will play a big role in
determining how successful you are in NET Compact
Framework programming—and how enjoyable you will find
it We have observed that the programmers who excel withdevice development are the ones who are able to see inthese limitations an enticing challenge and an opportunity
to explore new ways to solve old problems
We extend our very best wishes to you on your NET
Compact Framework development, whether for the Pocket
PC, the Smartphone, or some other Windows CE–poweredsmart device We look forward to seeing you in one of ourworkshops or at an industry conference, or trading
Trang 37Prev don't be afraid of buying books Next
Trang 38Any large project—and a book is nothing if not a large
project—takes a lot of time to do right What makes it
worthwhile is the support of the people you really careabout
Paul Yao wants to thank his dear wife, Rebecca, for hersupport and encouragement during the many months ittook to work on this book Paul also wants to acknowledgehis son, Jasper, who provided the time and space
necessary to work on the book and also the fun when Dadneeded time off from writing and coding Thanks also toGrandma Helen and Aunt Nancy for entertaining the troopswhile Paul was busy with the book
David Durant would like to thank his wife, Betty, who
endured it all while providing tranquility throughout
Thanks also to the staff at The Paul Yao Company for
believing in this project and helping to see it through tocompletion
At Addison-Wesley, thanks are due to Martin Heller, whofirst contacted us about this project and believed in us
enough to help push for the insanity of attempting to writetwo books as part of one writing project Thank you,
Martin; we owe you another lunch or two on Mulberry
Street!
We also thank Stephane Thomas, the Acquisitions Editorfor this project, for her work on getting approval for thisproject and for coordinating the many players that supportthe creation of a new manuscript Thanks also to
Stephane's assistant, Michael Mullen
Trang 39management and support during the book's production tokeep us on our tight schedule Thank you also to ChrystaMeadowbrooke for laser-like precision during the
copyediting process and helping us fine-tune our otherwiseunreadable text
At Microsoft, we got a lot of help and technical support Onthe NET Compact Framework team, thanks are due toSeth Demsey for prompt and friendly support for what
probably seemed like an endless stream of questions Wealso wish to thank Craig Neable, Jamie De Guerre, and BenAlbahari, who are also on the NET Compact Frameworkteam In the SQL Server CE group, we thank Kevin Collins
for help on the MSDN Magazine article, which serves as the
foundation for Chapters 12 and 13 On the Visual Studio.NET team, thanks are due to Mark Cliggett, Benjamin
Wulfe, and Ritchie Hughes
A big word of thanks to the extended Windows CE coreteam, which has always been quick to provide help andsupport when we had questions, comments, or just neededsomeone to buy us a cup of coffee Our thanks go to ToddBrix, Karen Carter, Jason Demeny, Mike Hall, Scott Horn,Jeana Jorgensen, Jay Loney, Mark Miller, Mark Mullen, ChipSchnarel, and Mike Thomson Thanks also are due to theMVP program for the help they have provided in keeping
us in touch with the inner workings of the Microsoft mothership: Michael Fosmire and Anthony Russell
We extend a special word of thanks to Robert Little on theMicrosoft Word team for help with taming Word for
Windows and for great answers and sample code to handleour many questions on macros, fields, and other word
processing magic
We thank Andy Harding of Intermec Corporation for his
Trang 40At Field Software, thanks are due to Tim Field for his helpand support getting information about the PrinterCE
development tool, which we discuss in Chapter 17
Perhaps not many readers are aware of this, but one
reason that Addison-Wesley books have such top-notchtechnical content is due to the extensive technical revieweach book receives prior to publication This involves hiringexperts on each subject to read chapters and to submitcritiques to the authors We found this a very valuable
process, and we wish to thank the reviewers who
participated in reviewing our book: Darrin Bishop, Bill
Draper, Alex Feinman, Fabio Ferracchiata, Gerard Frantz,Greg Hack, Doug Holland, Andrew Krowczyk, Ivo Salmre,Jonathan Wells, and Michael Yuan
In addition to the technical review of the text that
Addison-Wesley conducted, we made all the chapters ofthe book available at The Paul Yao Company Web site while
we were writing the book We were inspired by Bruce
Eckel, who pioneered the idea of open reviews for
programmer books We did this to solicit comments andsuggestions and were very pleased with the response we