!PPLIED 70& IN #ONTEXT
■■■ 2AFFAELE 'AROFALO
!PPLIED 70& IN #ONTEXT #OPYRIGHT Ú BY 2AFFAELE 'AROFALO !LL RIG...
13 downloads
623 Views
6MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
!PPLIED 70& IN #ONTEXT
■■■ 2AFFAELE 'AROFALO
!PPLIED 70& IN #ONTEXT #OPYRIGHT Ú BY 2AFFAELE 'AROFALO !LL RIGHTS RESERVED .O PART OF THIS WORK MAY BE REPRODUCED OR TRANSMITTED IN ANY FORM OR BY ANY MEANS ELECTRONIC OR MECHANICAL INCLUDING PHOTOCOPYING RECORDING OR BY ANY INFORMATION STORAGE OR RETRIEVAL SYSTEM WITHOUT THE PRIOR WRITTEN PERMISSION OF THE COPYRIGHT OWNER AND THE PUBLISHER )3". PBK )3". ELECTRONIC 0RINTED AND BOUND IN THE 5NITED 3TATES OF !MERICA 4RADEMARKED NAMES LOGOS AND IMAGES MAY APPEAR IN THIS BOOK 2ATHER THAN USE A TRADEMARK SYMBOL WITH EVERY OCCURRENCE OF A TRADEMARKED NAME LOGO OR IMAGE WE USE THE NAMES LOGOS AND IMAGES ONLY IN AN EDITORIAL FASHION AND TO THE BENEFIT OF THE TRADEMARK OWNER WITH NO INTENTION OF INFRINGEMENT OF THE TRADEMARK 4HE USE IN THIS PUBLICATION OF TRADE NAMES TRADEMARKS SERVICE MARKS AND SIMILAR TERMS EVEN IF THEY ARE NOT IDENTIFIED AS SUCH IS NOT TO BE TAKEN AS AN EXPRESSION OF OPINION AS TO WHETHER OR NOT THEY ARE SUBJECT TO PROPRIETARY RIGHTS 0RESIDENT AND 0UBLISHER 0AUL -ANNING ,EAD %DITOR *ONATHAN (ASSELL 4ECHNICAL 2EVIEWER $AMIEN &OGGON %DITORIAL "OARD 3TEVE !NGLIN -ARK "ECKNER %WAN "UCKINGHAM 'ARY #ORNELL *ONATHAN 'ENNICK *ONATHAN (ASSELL -ICHELLE ,OWMAN *AMES -ARKHAM -ATTHEW -OODIE *EFF /LSON *EFFREY 0EPPER &RANK 0OHLMANN $OUGLAS 0UNDICK "EN 2ENOW #LARKE $OMINIC 3HAKESHAFT -ATT 7ADE 4OM 7ELSH #OORDINATING %DITOR *ENNIFER , "LACKWELL #OPY %DITORS 3HARON 4ERDEMAN AND +IM 7IMPSETT 0RODUCTION 3UPPORT 0ATRICK #UNNINGHAM )NDEXER *ULIE 'RADY !RTIST !PRIL -ILNE #OVER $ESIGNER !NNA )SHCHENKO $ISTRIBUTED TO THE BOOK TRADE WORLDWIDE BY 3PRINGER 3CIENCE"USINESS -EDIA ,,# 3PRING 3TREET TH &LOOR .EW 9ORK .9 0HONE 302).'%2 FAX E MAIL ^aSTab]h/b_aX]VTabQ\R^\ OR VISIT fffb_aX]VTa^][X]TR^\ &OR INFORMATION ON TRANSLATIONS PLEASE E MAIL aXVWcb/P_aTbbR^\ OR VISIT fffP_aTbbR^\ !PRESS AND FRIENDS OF %$ BOOKS MAY BE PURCHASED IN BULK FOR ACADEMIC CORPORATE OR PROMOTIONAL USE E"OOK VERSIONS AND LICENSES ARE ALSO AVAILABLE FOR MOST TITLES &OR MORE INFORMATION REFERENCE OUR 3PECIAL "ULK 3ALESnE"OOK ,ICENSING WEB PAGE AT fffP_aTbbR^\Qd[ZbP[Tb 4HE INFORMATION IN THIS BOOK IS DISTRIBUTED ON AN hAS ISv BASIS WITHOUT WARRANTY !LTHOUGH EVERY PRECAUTION HAS BEEN TAKEN IN THE PREPARATION OF THIS WORK NEITHER THE AUTHORS NOR !PRESS SHALL HAVE ANY LIABILITY TO ANY PERSON OR ENTITY WITH RESPECT TO ANY LOSS OR DAMAGE CAUSED OR ALLEGED TO BE CAUSED DIRECTLY OR INDIRECTLY BY THE INFORMATION CONTAINED IN THIS WORK 4HE SOURCE CODE FOR THIS BOOK IS AVAILABLE TO READERS AT fffP_aTbbR^\ 9OU WILL NEED TO ANSWER QUESTIONS PERTAINING TO THIS BOOK IN ORDER TO SUCCESSFULLY DOWNLOAD THE CODE
Contents !BOUT THE !UTHOR XII !BOUT THE 4ECHNICAL 2EVIEWER XIII !CKNOWLEDGMENTS XIV )NTRODUCTION XV ■#HAPTER )NTRODUCING 70& AND 8!-, The XAML Syntax ............................................................................................................... 2 Namespaces and Root Elements .............................................................................................................. 2 Objects and Common Elements................................................................................................................ 3 The Code Behind....................................................................................................................................... 6 Attached Properties .................................................................................................................................. 8
Introduction to WPF ........................................................................................................... 8 WPF Architecture .................................................................................................................................... 13 WPF 4 Tools ............................................................................................................................................ 17
Summary ......................................................................................................................... 23 ■#HAPTER 3AMPLE !PPLICATION /VERVIEW AND 'ETTING 3TARTED Requirements .................................................................................................................. 25 Tools and Software ................................................................................................................................. 25 Icons and Templates .............................................................................................................................. 26 Third-Party Library and Controls ............................................................................................................ 26
Application Overview ....................................................................................................... 27 Application Architecture ......................................................................................................................... 27 User Stories ............................................................................................................................................ 31
V
■ #/.4%.43
Domain Model......................................................................................................................................... 32 Database Schema................................................................................................................................... 34
Application Configuration ................................................................................................ 35 Visual Studio Solution ............................................................................................................................. 35 Resources and Third-Party Components ................................................................................................ 38
Summary ......................................................................................................................... 41 ■#HAPTER -ICROSOFT %XPRESSION "LEND Overview .......................................................................................................................... 43 Blend Workspace ............................................................................................................. 45 Workspace Panels .................................................................................................................................. 46
Drawing ........................................................................................................................... 48 Shapes and Objects ................................................................................................................................ 49 Text and Text Effects .............................................................................................................................. 50
Animations ....................................................................................................................... 51 Styling and Templating Controls ...................................................................................... 53 Modifying Properties .............................................................................................................................. 54 Creating Styles ....................................................................................................................................... 55
Design-Time Data ............................................................................................................ 57 SketchFlow and the Mockups ......................................................................................... 59 Summary ......................................................................................................................... 60 ■#HAPTER #REATING THE 6IEWS Overview .......................................................................................................................... 61 Creating a Mock-up and Prototyping ............................................................................... 62 The Main Screen..................................................................................................................................... 64 The List Views ........................................................................................................................................ 66 The Details Views ................................................................................................................................... 69
VI
■ #/.4%.43
The View Models.............................................................................................................. 70 Base ViewModel Implementation ........................................................................................................... 70 Details ViewModel .................................................................................................................................. 73 List ViewModel ....................................................................................................................................... 74
Navigation Between the Views ........................................................................................ 77 The App.xaml File ................................................................................................................................... 77
Wrapping Up with IoC ...................................................................................................... 79 Inversion of Control ................................................................................................................................ 79 A Classic Implementation, Navigation Service ....................................................................................... 79
Summary ......................................................................................................................... 85 ■#HAPTER !DDING #ONTROLS TO THE 6IEWS Resources and Styles ...................................................................................................... 87 Differences Between Static and Dynamic Resources ............................................................................ 88 The Resource Dictionary......................................................................................................................... 88 Controls’ Styles....................................................................................................................................... 91
Creating the Views ........................................................................................................... 97 Login View .............................................................................................................................................. 98 Details View .......................................................................................................................................... 100 List View ............................................................................................................................................... 104 User Controls for the Dashboard .......................................................................................................... 107 The Dashboard (MainView) ................................................................................................................... 109
Add Features to the Application..................................................................................... 110 Custom Effects ..................................................................................................................................... 110
Summary ....................................................................................................................... 111 ■#HAPTER 4HE %NTITY &RAMEWORK Object Relational Mappers ................................................................................................................... 113 Active Record Pattern ........................................................................................................................... 115 Code First Development ....................................................................................................................... 117
VII
■ #/.4%.43
The TimeTracker Domain............................................................................................... 121 Mapping the Domain Model.................................................................................................................. 121
The TimeTracker Data Layer.......................................................................................... 135 The Unit of Work ................................................................................................................................... 135 The Repository ...................................................................................................................................... 151
Summary ....................................................................................................................... 158 ■#HAPTER $ATA "INDING WPF Data Binding Engine .............................................................................................. 159 The INotifyPropertyChanged Interface .................................................................................................. 160 Binding Direction .................................................................................................................................. 162 The IValueConverter Interface .............................................................................................................. 162 DataTemplate ....................................................................................................................................... 166 UI Validation.......................................................................................................................................... 168 Binding Syntax...................................................................................................................................... 170
ViewModels ................................................................................................................... 171 A Generic ViewModel Validator............................................................................................................. 171 Employee ViewModels.......................................................................................................................... 173 Additional ViewModels and Properties ................................................................................................. 184
Summary ....................................................................................................................... 187 ■#HAPTER #OMMAND (ANDLING AND %VENT 2OUTING Introduction to Commands ............................................................................................ 190 The Command Pattern .......................................................................................................................... 190 Command in WPF ................................................................................................................................. 191 Interactive Command, Listen for Changes............................................................................................ 198
TimeTracker Commands ................................................................................................ 204 The EmployeeListView.......................................................................................................................... 204
Summary ....................................................................................................................... 208
VIII
■ #/.4%.43
■#HAPTER 4ESTING WITH 4$$ Test Driven Development .............................................................................................. 209 Code Coverage. .................................................................................................................................... 210 TDD with MSTest . ................................................................................................................................ 212 Code Coverage with Visual Studio . ...................................................................................................... 216
TDD: the TimeTracker Application . ............................................................................... 219 Test the Business (Domain) Layer . ...................................................................................................... 220 Testing the Data Layer. ........................................................................................................................ 221 Testing the UI. ...................................................................................................................................... 223
Summary ....................................................................................................................... 225 ■#HAPTER 2EPORTS WITH -ICROSOFT 2EPORTING 3ERVICES SQL Server Reporting Services ..................................................................................... 228 Configure SQL Server Reporting Services ............................................................................................ 228
TimeTracker Reports . ................................................................................................... 231 First Report: EmployeeListReport . ....................................................................................................... 231 EmployeeDetailsReport . ...................................................................................................................... 237 Additional Reports . .............................................................................................................................. 241
Hosting the Reports in a WPF Control . .......................................................................... 241 Display the Report in a WPF View. ....................................................................................................... 242 Load a List of Employees Report . ........................................................................................................ 244 Load a Detailed Report . ....................................................................................................................... 246
Summary ....................................................................................................................... 248 ■#HAPTER $EPLOY THE !PPLICATION 5SING #LICK/NCE ClickOnce Deployment .................................................................................................. 251 ClickOnce Security. .............................................................................................................................. 252
IX
■ #/.4%.43
Publishing Options ......................................................................................................... 254 Updates ................................................................................................................................................ 254 Application Files and Prerequisites ...................................................................................................... 255 Additional Options ................................................................................................................................ 256
Deploy the TimeTracker Application .............................................................................. 257 Create a Temporary Certificate............................................................................................................. 258 Security ................................................................................................................................................ 259 Publish TimeTracker ............................................................................................................................. 259
Summary ....................................................................................................................... 260 ■#HAPTER $ESIGN 0ATTERNS IN 70& Patterns for the UI .......................................................................................................... 261 Model View Presenter........................................................................................................................... 262 Model View ViewModel ........................................................................................................................ 268
Summary ....................................................................................................................... 273 ■#HAPTER 70& AND -ULTITHREADING Writing Multithreading Code in .NET .............................................................................. 276 The Thread Object ................................................................................................................................ 276 Asynchronous Operations ..................................................................................................................... 280 The Task Object .................................................................................................................................... 283 The Background Worker Component .................................................................................................... 285
Summary ....................................................................................................................... 287 ■#HAPTER )NTERACTING WITH 7#& Introduction to WCF ....................................................................................................... 290 Brief Overview of WCF Architecture ..................................................................................................... 291
A Sample WCF Project ................................................................................................... 292 The WCF Test Client Utility ................................................................................................................... 294 Customize the Sample Service ............................................................................................................. 295
X
■ #/.4%.43
Test the Sample Service ....................................................................................................................... 300 Conclusion and Considerations ............................................................................................................ 304
Integrate WCF into the TimeTracker Application ........................................................... 304 WCF Data Service ................................................................................................................................. 304 Navigate the Data Using oData Queries................................................................................................ 306 Integrate the WCF Data Service in WPF................................................................................................ 307
WCF Security ................................................................................................................. 308 Security Requirements ......................................................................................................................... 309 Bindings................................................................................................................................................ 309 Provide the Credentials ........................................................................................................................ 310 Authenticate and Authorize the Code ................................................................................................... 310
Summary ....................................................................................................................... 311 )NDEX
XI
About the Author ■2AFFAELE 'AROFALO IS A .%4 SOFTWARE ARCHITECT WHO BUILDS LINE OF BUSINESS ,/" APPLICATIONS FOR A LIVING (E WORKS FOR A HEDGE FUND IN "ERMUDA WHERE HE AND HIS WIFE $EBBIE MOVED THREE YEARS AGO (E IS ORIGINALLY FROM )TALY AND IN HIS FREE TIME HE LOVES TO TRAVEL AROUND %UROPE WITH HIS WIFE 2AFFAELE IS PASSIONATE ABOUT .%4 AND 70& AND SPENDS HIS FREE TIME WRITING ARTICLES AND BLOG POSTS ABOUT 70& AND THE -66- (E IS -ICROSOFT #ERTIFIED -#!$ -#3$ -#43 31, 3ERVER AND 3HARE0OINT AND HE HOSTS ARTICLES AND BLOG POSTS ABOUT 70& AND -66- ON HIS BLOG 9OU CAN VISIT HIS BLOG AT Wcc_)Q[^VaPUUPTdR^\ 4HIS IS 2AFFAELE SECOND BOOK (IS FIRST ONE WAS "UILDING %NTERPRISE !PPLICATIONS WITH 7INDOWS 0RESENTATION &OUNDATION AND THE -ODEL 6IEW 6IEW-ODEL 0ATTERN -ICROSOFT 0RESS ABOUT ,/" APPLICATIONS AND THE -66- PATTERN
XII
Acknowledgments 4HIS IS ACTUALLY MY SECOND BOOK AND ) AM REALLY HAPPY AND PROUD TO BE AN !PRESS WRITER ) BUILT A SUBSTANTIAL PORTION OF MY SKILLS READING !PRESS BOOKS AND THE IDEA OF BEING PART OF THE !PRESS BOOKSHELF THRILLS ME /F COURSE WRITING A BOOK IS NOT AN EASY TASK AND IT TAKES A TEAM EVEN IF MY NAME IS THE ONLY ONE ON THE COVER OF THIS BOOK &IRST OF ALL ) HAVE TO THANK MY EDITOR *ONATHAN (ASSELL FOR TRUSTING ME AND GIVING ME THIS OPPORTUNITY 7ITHOUT HIM THIS BOOK WOULD PROBABLY NEVER HAVE BEEN WRITTEN -Y TECHNICAL REVIEWER $AMIEN &OGGON HAS BEEN AN AMAZING VOLCANO OF IDEAS AND SUGGESTIONS AND )M GRATEFUL FOR HIS HELP !ND MY COORDINATING EDITOR *ENNIFER "LACKWELL WAS THE ONE WHO MADE SURE WE GOT THE BOOK DONE IN TIME 4HANKS *ENNY &INALLY ) WANT TO THANK THE ENTIRE !PRESS TEAMTHEY ARE TRUE PROFESSIONALS ) HOPE ) WILL BE ABLE TO WRITE ANOTHER BOOK WITH THEM SOMETIME SOON ,AST BUT NOT LEAST ) THANK MY WIFE $EBBIE FOR HER SUPPORT AND DEDICATION 3HE IS MY PERSONAL PROJECT MANAGER AND IF ) HAVE COMPLETED TWO BOOKS IT IS ONLY BECAUSE ) HAVE A WONDERFUL AND UNDERSTANDING PARTNER WHO ALLOWS ME TO SPEND MUCH OF OUR FAMILY TIME ON THESE PROJECTS !LSO ) WANT TO THANK MY SWEETIE DOG -ORGANA WHO AFTER TEN YEARS LEFT ME JUST BEFORE ) FINISHED THIS BOOK )LL ALWAYS REMEMBER THE NIGHTS ) SPENT WITH HER ON THE SOFA GETTING THE BOOK DONE
XIV
Contents at a Glance !BOUT THE !UTHOR XII !BOUT THE 4ECHNICAL 2EVIEWER XIII !CKNOWLEDGMENTS XIV )NTRODUCTION XV ■#HAPTER )NTRODUCING 70& AND 8!-, ■#HAPTER 3AMPLE !PPLICATION /VERVIEW AND 'ETTING 3TARTED ■#HAPTER -ICROSOFT %XPRESSION "LEND ■#HAPTER #REATING THE 6IEWS ■#HAPTER !DDING #ONTROLS TO THE 6IEWS ■#HAPTER 4HE %NTITY &RAMEWORK ■#HAPTER $ATA "INDING ■#HAPTER #OMMAND (ANDLING AND %VENT 2OUTING ■#HAPTER 4ESTING WITH 4$$ ■#HAPTER 2EPORTS WITH -ICROSOFT 2EPORTING 3ERVICES ■#HAPTER $EPLOY THE !PPLICATION 5SING #LICK/NCE ■#HAPTER $ESIGN 0ATTERNS IN 70& ■#HAPTER 70& AND -ULTITHREADING ■#HAPTER )NTERACTING WITH 7#& )NDEX
IV
Introduction 7INDOWS 0RESENTATION &OUNDATION 70& IS A GRAPHICAL COMPUTER PLATFORM BUILT AND DISTRIBUTED BY -ICROSOFT TO CREATE RICH CLIENT APPLICATIONS FOR 7INDOWS SYSTEMS 7ITH 70& YOU CAN BUILD RICH INTERFACES WITH ANIMATIONS AND AMAZING GRAPHICAL EFFECTS AS EASILY AS YOU CAN BUILD MINIMALIST AND CORPORATE USER INTERFACES FOR LINE OF BUSINESS ,/" APPLICATIONS 5NFORTUNATELY BECAUSE 70& IS SUCH A POWERFUL AND COMPLEX TECHNOLOGY IT REQUIRES SOME INITIAL EFFORT TO UNDERSTAND ITS MECHANISMS -OREOVER FOR A NEWBIE THE 8!-, MARKUP USED TO CREATE THE USER INTERFACES CAN BE TOUGH TO COME TO GRIPS WITH #REATING A 70& APPLICATION AND MORE GENERALLY CREATING ANY TYPE OF APPLICATION WITH A USER INTERFACE IS A RATHER CONVOLUTED TASK IT COMPRISES A NUMBER OF PHASES AND THE FINAL RESULT IS LIKELY TO BE OF A CERTAIN COMPLEXITY ! STANDALONE APPLICATION BUILT USING 70& IS USUALLY MADE UP OF VARIOUS hCOMPONENTSv THAT ARE USED TO ENCAPSULATE THE hMODULESv OF THE SOFTWARE 9OU MIGHT HAVE FOR INSTANCE A COMPONENT TO ACCESS THE DATA A COMPONENT TO INCLUDE A LOGICAL FUNCTION A COMPONENT TO RENDER THE USER INTERFACE AND SO ON )N THIS BOOK ) WILL SHOW YOU HOW TO CREATE AND IMPLEMENT 70& USING BEST PRACTICES TO CREATE A REAL WORLD APPLICATION !T THE SAME TIME )LL ALSO SHOW YOU HOW TO STRUCTURE AND ARCHITECT A 70& APPLICATION MADE UP OF DIFFERENT COMPONENTS THAT YOULL BE ABLE TO RECYCLE FOR FUTURE APPLICATIONS "Y THE END OF THE BOOK YOU SHOULD HAVE A WORKING KNOWLEDGE OF 70& AND KNOW HOW TO ARCHITECT A 70& APPLICATION USING THE TOOLS PROVIDED BY -ICROSOFT SUCH AS 31, 3ERVER 2 %XPRESS %DITION THE %NTITY &RAMEWORK 7INDOW #OMMUNICATION &OUNDATION 7#& AND MORE
7HO 4HIS "OOK IS &OR 4HIS BOOK IS FOR 7INDOWS APPLICATION DEVELOPERS WHO WANT TO UNDERSTAND THE CONTEXT IN WHICH 70& SITS AND THE STANDARDS AND BEST PRACTICES THAT CAN BE EMPLOYED TO IMPROVE THE EFFICIENCY AND MAINTAINABILITY OF THEIR PROJECTS
(OW 4HIS "OOK IS 3TRUCTURED 4HE BOOK IS DIVIDED INTO CHAPTERS BUT THE OVERALL STRUCTURE FOLLOWS A SPECIFIC LOGIC SO ) SUGGEST YOU READ THE CHAPTERS SEQUENTIALLYESPECIALLY IF THE TOPIC IS NEW TO YOU 4HE BOOK EXPLAINS HOW TO BUILD A 70& APPLICATION FROM BEGINNING TO END 9OU WILL START BY ANALYZING THE USERS REQUIREMENTS BEFORE SETTING OUT TO CREATE THE APPLICATION AND THEN YOULL LEARN HOW TO WORK WITH -ICROSOFT %XPRESSION "LEND TO LAY OUT THE USER INTERFACE THAT IS THE ESSENCE OF THE APPLICATION /N THE WAY YOULL LEARN ABOUT !GILE DEVELOPMENT CONCEPTS SUCH AS $OMAIN $RIVEN $ESIGN /BJECT 2ELATIONAL -APPERS THE BUSINESS LAYER AND 3ERVICE /RIENTED !RCHITECTURE 4HERE IS EVEN ONE CHAPTER DEDICATED TO MULTITHREADING AND PARALLEL PROGRAMMING IN 70& AND MORE GENERALLY WITH THE .%4 &RAMEWORK AND ) HAVE ALSO DEDICATED A CHAPTER TO THE REPORTING TOOL OFFERED FOR FREE WITH -ICROSOFT 31, 3ERVER 2 %XPRESS EDITION 31, 3ERVER 2EPORTING 3ERVICES !T THE END OF THE BOOK YOULL BE ABLE TO DEPLOY THE APPLICATION USING #LICK/NCE AND ))3 AND YOULL UNDERSTAND HOW 70&S DISTRIBUTION MECHANISM WORKS
XV
■ ).42/$5#4)/.
$OWNLOADING THE #ODE 4HE SOURCE CODE FOR THIS BOOK IS AVAILABLE TO READERS AT fffP_aTbbR^\ IN THE $OWNLOAD SECTION OF THE BOOKS PAGE 0LEASE VISIT THE !PRESS WEB SITE TO DOWNLOAD ALL THE CODE 9OU CAN ALSO CHECK FOR ERRATA AND FIND RELATED !PRESS TITLES
#ONTACTING THE !UTHOR 9OU ARE WELCOME TO CONTACT THE AUTHOR 2AFFAELE 'AROFALO AKA 2AFFAEU WITH QUESTIONS 9OU CAN REACH HIM AT aPUUPTd/aPUUPTdR^\ OR YOU CAN CONTACT HIM BY VISITING Wcc_)Q[^VaPUUPTdR^\ &OLLOW HIM ON TWITTER /APUUPTd
XVI
CHAPTER 1 ■■■
Introducing WPF and XAML $EVELOPED BY -ICROSOFT 7INDOWS 0RESENTATION &OUNDATION 70& IS A GRAPHICAL SUBSYSTEM FOR RENDERING USER INTERFACES IN A 7INDOWS OPERATING SYSTEM 70& IS BASED ON A MARKUP LANGUAGE ALSO KNOWN AS 8!-, 8!-, WHICH IS AN ACRONYM FOR %XTENSIBLE !PPLICATION -ARKUP ,ANGUAGE IS A DECLARATIVE MARKUP LANGUAGE AVAILABLE IN THE .%4 &RAMEWORK DESIGNED TO SIMPLIFY THE 5) CREATION 8!-, IS APPLIED TO DIFFERENT TECHNOLOGIES IT IS AVAILABLE FOR 7INDOWS 0RESENTATION &OUNDATION 3ILVERLIGHT AND 7INDOWS 7ORKFLOW &OUNDATION 7& 4HE PRINCIPAL REASON OF HAVING A MARKUP LANGUAGE FOR THE 5) CREATION IS TO DECOUPLE THE 5) CODE FROM THE PRESENTATION LOGIC THAT CAN STILL BE WRITTEN IN ONE OF THE AVAILABLE .%4 LANGUAGES SUCH AS # OR 6" .%4 5SING THIS APPROACH YOU CAN ASSIGN THE 5) DEVELOPMENT PROCESS TO A DEVELOPERDESIGNER MORE SPECIALIZED IN THE 5) CREATION WHO WILL PROBABLY USE %XPRESSION "LEND AND YOU CAN LEAVE THE CORE DEVELOPMENT PROCESS TO A .%4 DEVELOPER WHO WILL PROBABLY ACCOMPLISH THIS TASK USING 6ISUAL 3TUDIO 5SUALLY BUT NOT ALWAYS A 8!-, FILE WILL HAVE AN EXTENSION OF TYPE gP\[ AND AN ENCODING OF TYPE 54& ,ISTING SHOWS THE 8!-, CODE TO CREATE A 70& CONTROL 4HIS SPECIFIC CODE IS DECLARING A BcPRZ?P]T[ CONTROL THAT HAS A ;Xbc1^g CONTROL INSIDE THE ;Xbc1^g THREE ITEMS ARE DEFINED EACH ONE USING A ;Xbc1^g8cT\ ELEMENT ,ISTING 3AMPLE 8!-, #ODE 5SED TO #REATE A "UTTON #ONTROL +BcPRZ?P]T[+;Xbc1^g+;Xbc1^g8cT\ 2^]cT]c,>]T +;Xbc1^g8cT\ 2^]cT]c,Cf^ 8bBT[TRcTS,CadT +;Xbc1^g8cT\ 2^]cT]c,CWaTT +;Xbc1^g+BcPRZ?P]T["EFORE STARTING TO LEARN HOW 70& WORKS YOU SHOULD ANALYZE IN MORE DETAIL THE 8!-, SYNTAX WHICH IS PART OF THE 70& TECHNOLOGY
#(!04%2 ■ ).42/$5#).' 70& !.$ 8!-,
■ .OTE %SPECIALLY IF YOU HAVE ALREADY WORKED IN THE PAST WITH 8!-, YOU MAY FIND THIS INTRODUCTION REPETITIVE 4HIS IS NOT BECAUSE ) HAD THE INTENTION OF WRITING A VERBOSE INTRODUCTION BUT BECAUSE THE 8!-, TECHNOLOGY AND SYNTAX ARE UNIQUE ACROSS DIFFERENT TECHNOLOGIES SO IF YOU EVER WORKED IN THE PAST WITH A 8!-, TECHNOLOGY LIKE 3ILVERLIGHT OR 7ORKFLOW &OUNDATION YOU MAY ALREADY UNDERSTAND THE MATERIAL IN THE NEXT SECTIONS
The XAML Syntax 4HE 8!-, LANGUAGE IS A COMPLEX AND VERY FLEXIBLE MARKUP LANGUAGE APPLIED TO DIFFERENT TECHNOLOGIES BY USING DIFFERENT REFERENCES IN THE 8!-, FILE SO THAT YOU CAN REFER TO DIFFERENT OBJECTS THAT PROVIDE DIFFERENT 8!-, ELEMENTS AND ATTRIBUTES ,IKE ANY OTHER 8-, FILE A 8!-, FILE MUST HAVE A VALID STRUCTURE AND MUST FOLLOW SOME SPECIFIC RULES ONE OF THESE RULES IS THE PRESENCE OF A VALID ROOT ELEMENT
.AMESPACES AND 2OOT %LEMENTS )N 8!-, YOU DEFINE A ROOT ELEMENT AS THE ROOT OF YOUR 8!-, DOCUMENT THIS IS A MANDATORY REQUIREMENT FOR THE 8!-, FILE AND FOR THE 8-, VALIDATION )N A 70& CONTEXT THE ROOT ELEMENT OF A 8!-, FILE WILL BE A ?PVT DbTa2^]ca^[ OR FX]S^f IF YOU ARE CREATING A 5) CONTAINER OR A SIMPLE ATb^daRT3XRcX^]Pah IF YOU ARE CREATING A COLLECTION OF RESOURCES FOR YOUR APPLICATION WHICH IS ANOTHER VALID ROOT ELEMENT FOR A 70& APPLICATION AND WHICH IS ALSO A STANDARD FILE CREATED BY THE 6ISUAL 3TUDIO PROJECT 4HESE ROOT ELEMENTS ARE ONLY SOME OF THE AVAILABLE OPTIONS FOR A 70& 8!-, FILE
■ .OTE )F YOU ARE CREATING A 8!-, FILE FOR ANOTHER TECHNOLOGY LIKE 3ILVERLIGHT OR 7ORKFLOW YOU WILL PROBABLY USE DIFFERENT ROOT ELEMENTS PROVIDED BY THESE TECHNOLOGIES
,ISTING SHOWS THE STRUCTURE OF A FX]S^f OBJECT WHICH CONSTITUTES A NORMAL WINDOW 5) IN 70& AND IS REPRESENTED BY THE ROOT ELEMENT FX]S^f ,ISTING 8!-, 2OOT .AMESPACES +FX]S^f g)2[Pbb,2WP_cTa U2^]eTacL
Property Element Syntax 3OMETIMES YOU MAY FIND IT DIFFICULT TO REPRESENT A COMPLEX TYPE AS A BcaX]V VALUE FOR AN ELEMENT PROPERTY VALUE SO THE VALUE SHOULD BE DECLARED USING A DIFFERENT TECHNIQUE &OR EXAMPLE A 1dcc^] HAS A 2^]cT]c PROPERTY THAT REPRESENTS THE CONTENT OF THE 1dcc^] )N 8!-, THE CONTENT CAN BE SIMPLE LIKE A BcaX]V OR COMPLEX LIKE A ?P]T[ WITH NESTED 5) CONTROLS )F YOU WANT TO ADD SUCH A KIND OF CONTENT IN A 1dcc^] YOU MAY NEED TO USE THE PROPERTY SYNTAX ELEMENT DEFINITION WHERE THE PROPERTY OF AN ELEMENT IS SPECIFIED AS AN ADDITIONAL ELEMENT A CHILD ONE 4HE FOLLOWING CODE SHOWS A 1dcc^] WITH A COMPLEX TYPE FOR ITS 2^]cT]c PROPERTY )N THIS EXAMPLE YOU ARE REPRESENTING THE CONTENT OF A 1dcc^] USING A BcPRZ?P]T[ THAT INCLUDES TWO CONTROLS AN 8\PVT THAT WILL DISPLAY THE BUTTONS ICON AND A CTgc1[^RZ THAT WILL DISPLAY THE BUTTONS CAPTION +1dcc^] 7TXVWc,# FXScW, !+1dcc^]2^]cT]c+BcPRZ?P]T[ >aXT]cPcX^],7^aXi^]cP[+8\PVT B^daRT,W^\TNV^N"!_]V <PaVX],$ +CTgc1[^RZ ETacXRP[0[XV]\T]c,2T]cTa CTgc,7^\T?PVT+BcPRZ?P]T[+1dcc^]2^]cT]c+1dcc^]4HE PROPERTY ELEMENT SYNTAX IS REPRESENTED BY THE NAME OF THE PARENT ELEMENT FOLLOWED BY A DOT THAT IS FOLLOWED BY THE NAME OF THE PROPERTY )N THIS WAY THE PROPERTY ELEMENT BECOMES A COMPLETE 8!-, ELEMENT THAT CAN INCLUDE A CHILD STRUCTURE 9OU CAN USE THE SAME TECHNIQUE TO DECLARE A COLLECTION OF VALUES )F THE PROPERTY ELEMENT YOU ARE WORKING WITH IS OF TYPE R^[[TRcX^] YOU CAN DECLARE A SET OF SEQUENTIAL CHILD ELEMENTS INSIDE IT LIKE IN THE FOLLOWING CODE WHICH IS DECLARING A SET OF CHILD ITEMS FOR A ;Xbc1^g ELEMENT +;Xbc1^g+;Xbc1^g8cT\b+;Xbc1^g8cT\ 2^]cT]c,>]T +;Xbc1^g8cT\ 2^]cT]c,Cf^ 8bBT[TRcTS,CadT +;Xbc1^g8cT\ 2^]cT]c,CWaTT +;Xbc1^g8cT\b+;Xbc1^g-
#(!04%2 ■ ).42/$5#).' 70& !.$ 8!-,
!S YOU CAN SEE THE 8cT\b PROPERTY HAS A COLLECTION OF ITEMS OF TYPE ;Xbc1^g8cT\ DECLARED SEQUENTIALLY IN THE PROPERTY ELEMENT
The Content Property )N 8!-, THERE IS A PARTICULAR PROPERTY AVAILABLE FOR ANY ELEMENT WHICH IS THE DEFAULT PROPERTY THAT IS DEFINED BY 2^]cT]c?a^_Tach0ccaXQdcT IF THE ELEMENT REPRESENTS A CLASS DERIVED FROM 2^]ca^[b 2^]cT]c2^]ca^[ THE DEFAULT PROPERTY IS OF TYPE 2^]cT]c?a^_Tach FOR OTHER ELEMENTS LIKE THE CTgc1[^RZ THE DEFAULT PROPERTY IS THE 8][X]Tb PROPERTY AND SO ON !NY CLASS REPRESENTED BY 8!-, CAN HAVE ONE AND ONLY ONE CONTENT PROPERTY THAT CAN BE CONSIDERED AS THE DEFAULT PROPERTY FOR THAT CLASS "ECAUSE THIS PROPERTY IS THE DEFAULT PROPERTY OF A CLASS YOU CAN TOTALLY OMIT ITS DECLARATION IN 8!-, IN ORDER TO USE IT IN THIS WAY YOU SHOULD SIMPLY ADD CONTENT INSIDE THE CLASS ELEMENT /F COURSE NOT EVERY CONTENT PROPERTY IS THE SAME FOR EXAMPLE YOU MAY HAVE A CONTENT PROPERTY OF TYPE BcaX]V AND A CONTENT PROPERTY OF TYPE >QYTRc ,ISTING SHOWS HOW YOU CAN IMPLICITLY OR EXPLICITLY DECLARE THE CONTENT VALUE OF A CTgc1[^RZ IN 8!-, ,ISTING 8!-, )MPLICIT AND %XPLICIT #ONTENT $ECLARATION +BcPRZ?P]T[+ 8