Windows Phone Recipes, 2nd Edition
Avoid the common pitfalls of Windows Phone programming with this concise recipe collection written specifically for the busy developer
- • • • •
Windows Phone
- • Recipes
A Problem-Solution Approach
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
WindowsPhoneRecipes
AProblem-SolutionApproach
FabioClaudioFerracchiati EmanueleGarofalo Copyright©2011byFabioClaudioFerracchiatiandEmanueleGarofalo Allrightsreserved.Nopartofthisworkmaybereproducedortransmittedinanyformorbyanymeans, electronicormechanical,includingphotocopying,recording,orbyanyinformationstorageorretrieval system,withoutthepriorwrittenpermissionofthecopyrightownerandthepublisher.
ISBN-13(pbk):978-1-4302-3371-8
ISBN-13(electronic):978-1-4302-3372-5 Trademarkednames,logos,andimagesmayappearinthisbook.Ratherthanuseatrademarksymbol witheveryoccurrenceofatrademarkedname,logo,orimage,weusethenames,logos,andimagesonly inaneditorialfashionandtothebenefitofthetrademarkowner,withnointentionofinfringementof thetrademark. Theuseinthispublicationoftradenames,trademarks,servicemarks,andsimilarterms,eveniftheyare notidentifiedassuch,isnottobetakenasanexpressionofopinionastowhetherornottheyaresubject toproprietaryrights.
PresidentandPublisher:PaulManning LeadEditor:EwanBuckingham TechnicalReviewer:SimonaNasetti EditorialBoard:SteveAnglin,MarkBeckner,EwanBuckingham,GaryCornell,MorganErtel,
JonathanGennick,JonathanHassell,RobertHutchinson,MichelleLowman,JamesMarkham, MatthewMoodie,JeffOlson,JeffreyPepper,DouglasPundick,BenRenow-Clarke,Dominic Shakeshaft,GwenanSpearing,MattWade,TomWelsh
CoordinatingEditor:JessicaBelanger CopyEditor:KimberlyBurton CoverDesigner:AnnaIshchenko Compositor:BythewayPublishingServices Indexer:SPIGlobal Artist:SPIGlobal
DistributedtothebooktradeworldwidebySpringerScience+BusinessMedia,LLC.,233SpringStreet, 6thFloor,NewYork,NY10013.Phone1-800-SPRINGER,fax(201)348-4505,e-mail Forinformationontranslations ApressandfriendsofEDbooksmaybepurchasedinbulkforacademic,corporate,orpromotionaluse. eBookversionsandlicensesarealsoavailableformosttitles.Formoreinformation,referenceour SpecialBulkSales—eBookLicensingwe Theinformationinthisbookisdistributedonan“asis”basis,withoutwarranty.Althoughevery precautionhasbeentakeninthepreparationofthiswork,neithertheauthor(s)norApressshallhave anyliabilitytoanypersonorentitywithrespecttoanylossordamagecausedorallegedtobecaused directlyorindirectlybytheinformationcontainedinthiswork. Thesourcecodeforthisbookisneedtoanswer questionspertainingtothisbookinordertosuccessfullydownloadthecode.
ToSimonaandClaudia,womenofmine —Fabio Tomymommyanddaddy,mylighthouseinthefog— Emanuele
Contents at a Glance
iv
AbouttheAuthors................................................................................................
AbouttheTechnicalReviewer ............................................................................ AboutThisBook................................................................................................. Chapter2:WindowsPhoneExecutionModelAndMultitasking ........................... Chapter4:UserInterfacewithExpressionBlend .............................................. Chapter6:Sensors ............................................................................................. Chapter8:IsolatedStorage ................................................................................ Chapter10:TestingWindowsPhoneApplications ............................................. Index ...................................................................................................................
Contents
AbouttheAuthor..................................................................................................
AbouttheTechnicalReviewer ............................................................................
AboutThisBook.................................................................................................
1-1.ExaminingWindowsPhone ........................................................................................ Problem ....................................................................................................................................................
1-2.UnderstandingtheDevelopmentTools ...................................................................... Problem .................................................................................................................................................... HowItWorks ............................................................................................................................................
1-3.CreatingaSimpleSilverlightWindowsPhoneApplication ........................................ Problem .................................................................................................................................................... HowItWorks ............................................................................................................................................
1-4.CreatingaSimpleXNAWindowsPhoneApplication................................................ Problem .................................................................................................................................................. HowItWorks .......................................................................................................................................... v
Usage...................................................................................................................................................... 1-5.CreatingaSimpleXNAandSilverlightMixedApplication........................................
Problem .................................................................................................................................................. HowItWorks ..........................................................................................................................................
1-6.DeployingtheApplicationtotheWindowsPhoneEmulator .................................... Problem .................................................................................................................................................. Usage......................................................................................................................................................
1-7.DeployingtheWindowsPhoneApplicationontheDevice ....................................... Problem .................................................................................................................................................. HowItWorks ..........................................................................................................................................
1-8.SendingaWindowsPhoneApplicationtotheMarketplace..................................... Problem .................................................................................................................................................. HowItWorks ..........................................................................................................................................
1-9.CreatingaTrialWindowsPhoneApplication ........................................................... Problem .................................................................................................................................................. HowItWorks ..........................................................................................................................................
Chapter2:WindowsPhoneExecutionModelAndMultitasking ........................... 2-1.NavigatingBetweenPages ......................................................................................
Problem .................................................................................................................................................. vi
HowItWorks ..........................................................................................................................................
Usage......................................................................................................................................................
2-2.PassingDataThroughPages ...................................................................................
Problem ..................................................................................................................................................
HowItWorks ..........................................................................................................................................
Usage......................................................................................................................................................
2-3.NavigatingBetweenPagesUsingGlobalApplicationVariables...............................
Problem ..................................................................................................................................................
HowItWorks ..........................................................................................................................................
Usage......................................................................................................................................................
2-4.NavigatingBetweenPageswithState .....................................................................
Problem ..................................................................................................................................................
HowItWorks ..........................................................................................................................................
Usage......................................................................................................................................................
2-5.ManagingTombstoninginWindowsPhoneSilverlightApplications........................
Problem ..................................................................................................................................................
HowItWorks ..........................................................................................................................................
Usage......................................................................................................................................................
2-6.ManagingTombstoninginWindowsPhoneXNAApplications................................. vii
Problem .................................................................................................................................................. HowItWorks .......................................................................................................................................... Usage......................................................................................................................................................
2-7.ImplementingMultitasking ...................................................................................... Problem .................................................................................................................................................. HowItWorks .......................................................................................................................................... Usage......................................................................................................................................................
2-8.SchedulingAlarmsandReminders .......................................................................... Problem .................................................................................................................................................. HowItWorks .......................................................................................................................................... Usage......................................................................................................................................................
2-9.ManagingObscuredandUnobscuredEvents........................................................... Problem .................................................................................................................................................. HowItWorks .......................................................................................................................................... Usage......................................................................................................................................................
Chapter3:UserInterface...................................................................................... 3-1.CreatinganAnimatedSplashScreen.......................................................................
Problem .................................................................................................................................................. HowItWorks .......................................................................................................................................... viii
TheCode.................................................................................................................................................
3-2.UsingtheApplicationBarControl ...........................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
3-3.DetectingChangesintheThemeTemplate ...........................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
3-4.CustomizingtheSoftInputPanelKeyboardtoAcceptOnlyNumbers ...................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
3-5.UsingtheWindowsPhonePredefinedStyles.........................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
3-6.LocalizingYourApplication ....................................................................................
Problem ................................................................................................................................................
ixSolution ................................................................................................................................................ TheCode...............................................................................................................................................
3-7.UsingPanoramaandPivotControls ....................................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
3-8.SpicingUptheUserInterfacewiththeSilverlightToolkit...................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
3-9.UsingLaunchersandChoosers.............................................................................. Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
3-10.AccessingtotheContactsLibrary........................................................................ Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage.................................................................................................................................................... x
3-11.AddingAdvertisingtoYourApplication................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
Chapter4:UserInterfacewithExpressionBlend ..............................................
4-1.GettingReadyforLightandDark ...........................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
4-2.ChangingtheSkinofYourApp...............................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
4-3.CreatingSomeCoolAnimation...............................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
4-4.CustomizingaControl ............................................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
xiTheCode............................................................................................................................................... Chapter5:Gestures ............................................................................................
5-1.ManagingGesturesinaSilverlightWindowsPhoneApplication ........................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
5-2.AddingGesturesManagementtoClick-lessSilverlightControls ........................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
5-3.HandlingGesturesinaGraphicalContext,SuchasaGameMenu ........................ Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
5-4.ManagingGesturesfromtheSilverlightforWindowsPhoneToolkit..................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage.................................................................................................................................................... xii
Chapter6:Sensors .............................................................................................
6-1.ResettingaFormbyShakingthePhone ................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
6-2.CreatingaSeismograph.........................................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
6-3.IndicatingtheUser’sPositiononaMap.................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
6-4.DisplayingSunsetandSunrise...............................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
6-5.IndicatingtheUser’sPositionviaCoordinates.......................................................
Problem ................................................................................................................................................
xiiiHowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
6-6.UsingtheRadio ...................................................................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
6-7.BuildinganAugmentedRealityApplication ........................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
Chapter7:MediaManagement........................................................................... 7-1.TakingaPhotofromYourPhoneCamera ..............................................................
Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
7-2.CapturingPhotoCameraVideoFrames.................................................................. Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage.................................................................................................................................................... xiv
7-3.PickingaPhotofromYourMediaLibrary...............................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
7-4.AddIntegrationBetweenYourApplicationandWindowsPhone ...........................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
7-5.UsingMediaPlayertoShuffleSongsinYourMediaLibrary ..................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
7-6.PlayingMusicStreamedfromSkyDrive.................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
7-7.UsingtheMicrophoneintheFunnyRepeaterApplication .....................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
xvTheCode............................................................................................................................................... 7-8.UsingtheMediaElementControltoPlayBothMusicandVideo ............................
Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
7-9.AddingIntegrationwiththeMusic-VideosHub...................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
Chapter8:IsolatedStorage ................................................................................ 8-1.SavingandLoadingaFileinIsolatedStorage .......................................................
Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
8-2.SavingSerializedData ........................................................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
8-3.ImplementingtheLocalDatabase.......................................................................... xvi
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
8-4.ModifyingtheSettingsofYourApplication ............................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
8-5.SavingaPhotoinIsolatedStorage ........................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
8-6.UsingIsolatedStorageExplorer .............................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
Usage....................................................................................................................................................
8-7.ImplementingBackgroundFileTransfer................................................................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................
xviiUsage.................................................................................................................................................... Chapter9:IntheCloud .......................................................................................
9-1.InteractingwithWCF.............................................................................................. Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
9-2.AccessingODataServices...................................................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
9-3.CreatingaFeedReader.......................................................................................... Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
9-4.CreatingaGoogleAPI–basedTranslator................................................................ Problem ................................................................................................................................................ HowItWorks ........................................................................................................................................ Usage....................................................................................................................................................
9-5.PushingYourNotification....................................................................................... Problem ................................................................................................................................................ xviii
Solution ................................................................................................................................................
TheCode...............................................................................................................................................
Chapter10:TestingWindowsPhoneApplications .............................................
10-1.ImplementingMVVMonWindowsPhoneUsingMVVMLight...............................
Problem ................................................................................................................................................
HowItWorks ........................................................................................................................................