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.

  ฀ Windows฀Phone฀Recipes฀

A฀Problem-Solution฀Approach฀

  ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀฀฀฀฀฀฀฀฀฀฀ Fabio฀Claudio฀Ferracchiati฀ Emanuele฀Garofalo฀ ฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀ Copyright฀©฀2011฀by฀Fabio฀Claudio฀Ferracchiati฀and฀Emanuele฀Garofalo฀ All฀rights฀reserved.฀No฀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.฀

  ISBN-13฀(pbk):฀978-1-4302-3371-8฀

  ISBN-13฀(electronic):฀978-1-4302-3372-5฀ Trademarked฀names,฀logos,฀and฀images฀may฀appear฀in฀this฀book.฀Rather฀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.฀ The฀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.฀

  President฀and฀Publisher:฀Paul฀Manning฀ Lead฀Editor:฀Ewan฀Buckingham฀ Technical฀Reviewer:฀Simona฀Nasetti฀ Editorial฀Board:฀Steve฀Anglin,฀Mark฀Beckner,฀Ewan฀Buckingham,฀Gary฀Cornell,฀Morgan฀Ertel,฀

  Jonathan฀Gennick,฀Jonathan฀Hassell,฀Robert฀Hutchinson,฀Michelle฀Lowman,฀James฀Markham,฀ Matthew฀Moodie,฀Jeff฀฀Olson,฀Jeffrey฀Pepper,฀Douglas฀Pundick,฀Ben฀Renow-Clarke,฀Dominic฀ Shakeshaft,฀Gwenan฀Spearing,฀Matt฀Wade,฀Tom฀Welsh฀

  Coordinating฀Editor:฀Jessica฀Belanger฀ Copy฀Editor:฀Kimberly฀Burton฀ Cover฀Designer:฀Anna฀Ishchenko฀ Compositor:฀Bytheway฀Publishing฀Services฀ Indexer:฀SPI฀Global฀ Artist:฀SPI฀Global฀

  Distributed฀to฀the฀book฀trade฀worldwide฀by฀Springer฀Science+Business฀Media,฀LLC.,฀233฀Spring฀Street,฀ 6th฀Floor,฀New฀York,฀NY฀10013.฀Phone฀1-800-SPRINGER,฀fax฀(201)฀348-4505,฀e-mail฀฀ For฀information฀on฀translations Apress฀and฀friends฀of฀ED฀books฀may฀be฀purchased฀in฀bulk฀for฀academic,฀corporate,฀or฀promotional฀use.฀ eBook฀versions฀and฀licenses฀are฀also฀available฀for฀most฀titles.฀For฀more฀information,฀reference฀our฀ Special฀Bulk฀Sales—eBook฀Licensing฀we The฀information฀in฀this฀book฀is฀distributed฀on฀an฀“as฀is”฀basis,฀without฀warranty.฀Although฀every฀ precaution฀has฀been฀taken฀in฀the฀preparation฀of฀this฀work,฀neither฀the฀author(s)฀nor฀Apress฀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.฀฀ The฀source฀code฀for฀this฀book฀isneed฀to฀answer฀ questions฀pertaining฀to฀this฀book฀in฀order฀to฀successfully฀download฀the฀code.฀

  To฀Simona฀and฀Claudia,฀women฀of฀mine฀฀ —Fabio฀ To฀my฀mommy฀and฀daddy,฀my฀lighthouse฀in฀the฀fog฀— Emanuele฀

  

Contents at a Glance

  iv฀

   ฀About฀the฀Authors................................................................................................

   ฀About฀the฀Technical฀Reviewer ............................................................................  ฀About฀This฀Book.................................................................................................  ฀ Chapter฀2:฀Windows฀Phone฀Execution฀Model฀And฀Multitasking ...........................  ฀ Chapter฀4:฀User฀Interface฀with฀฀Expression฀Blend ..............................................  ฀ Chapter฀6:฀Sensors .............................................................................................  ฀ Chapter฀8:฀Isolated฀Storage ................................................................................  ฀ Chapter฀10:฀Testing฀Windows฀Phone฀Applications .............................................  ฀ Index ...................................................................................................................

Contents

  

฀About฀the฀Author..................................................................................................

  

 ฀About฀the฀Technical฀Reviewer ............................................................................

 ฀About฀This฀Book.................................................................................................

  1-1.฀Examining฀Windows฀Phone ........................................................................................ Problem ....................................................................................................................................................

  1-2.฀Understanding฀the฀Development฀Tools ...................................................................... Problem .................................................................................................................................................... How฀It฀Works ............................................................................................................................................

  1-3.฀Creating฀a฀Simple฀Silverlight฀Windows฀Phone฀Application ........................................ Problem .................................................................................................................................................... How฀It฀Works ............................................................................................................................................

  1-4.฀Creating฀a฀Simple฀XNA฀Windows฀Phone฀Application................................................ Problem .................................................................................................................................................. How฀It฀Works .......................................................................................................................................... v฀

  Usage...................................................................................................................................................... 1-5.฀Creating฀a฀Simple฀XNA฀and฀Silverlight฀Mixed฀Application........................................

  Problem .................................................................................................................................................. How฀It฀Works ..........................................................................................................................................

  1-6.฀Deploying฀the฀Application฀to฀the฀Windows฀Phone฀Emulator .................................... Problem .................................................................................................................................................. Usage......................................................................................................................................................

  1-7.฀Deploying฀the฀Windows฀Phone฀Application฀on฀the฀Device ....................................... Problem .................................................................................................................................................. How฀It฀Works ..........................................................................................................................................

  1-8.฀Sending฀a฀Windows฀Phone฀Application฀to฀the฀Marketplace..................................... Problem .................................................................................................................................................. How฀It฀Works ..........................................................................................................................................

  1-9.฀Creating฀a฀Trial฀Windows฀Phone฀Application ........................................................... Problem .................................................................................................................................................. How฀It฀Works ..........................................................................................................................................

   ฀ Chapter฀2:฀Windows฀Phone฀Execution฀Model฀And฀Multitasking ........................... 2-1.฀Navigating฀Between฀Pages ......................................................................................

  Problem .................................................................................................................................................. vi฀

  

How฀It฀Works ..........................................................................................................................................

Usage......................................................................................................................................................

  2-2.฀Passing฀Data฀Through฀Pages ...................................................................................

Problem ..................................................................................................................................................

How฀It฀Works ..........................................................................................................................................

Usage......................................................................................................................................................

  2-3.฀Navigating฀Between฀Pages฀Using฀Global฀Application฀Variables...............................

Problem ..................................................................................................................................................

How฀It฀Works ..........................................................................................................................................

Usage......................................................................................................................................................

  2-4.฀Navigating฀Between฀Pages฀with฀State .....................................................................

Problem ..................................................................................................................................................

How฀It฀Works ..........................................................................................................................................

Usage......................................................................................................................................................

  2-5.฀Managing฀Tombstoning฀in฀Windows฀Phone฀Silverlight฀Applications........................

Problem ..................................................................................................................................................

How฀It฀Works ..........................................................................................................................................

Usage......................................................................................................................................................

  2-6.฀Managing฀Tombstoning฀in฀Windows฀Phone฀XNA฀Applications................................. vii฀

  Problem .................................................................................................................................................. How฀It฀Works .......................................................................................................................................... Usage......................................................................................................................................................

  2-7.฀Implementing฀Multitasking ...................................................................................... Problem .................................................................................................................................................. How฀It฀Works .......................................................................................................................................... Usage......................................................................................................................................................

  2-8.฀Scheduling฀Alarms฀and฀Reminders .......................................................................... Problem .................................................................................................................................................. How฀It฀Works .......................................................................................................................................... Usage......................................................................................................................................................

  2-9.฀Managing฀Obscured฀and฀Unobscured฀Events........................................................... Problem .................................................................................................................................................. How฀It฀Works .......................................................................................................................................... Usage......................................................................................................................................................

   ฀ Chapter฀3:฀User฀Interface...................................................................................... 3-1.฀Creating฀an฀Animated฀Splash฀Screen.......................................................................

  Problem .................................................................................................................................................. How฀It฀Works .......................................................................................................................................... viii฀

  

The฀Code.................................................................................................................................................

3-2.฀Using฀the฀ApplicationBar฀Control ...........................................................................

  

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  3-3.฀Detecting฀Changes฀in฀the฀Theme฀Template ...........................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  3-4.฀Customizing฀the฀Soft฀Input฀Panel฀Keyboard฀to฀Accept฀Only฀Numbers ...................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  3-5.฀Using฀the฀Windows฀Phone฀Predefined฀Styles.........................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  3-6.฀Localizing฀Your฀Application ....................................................................................

Problem ................................................................................................................................................

ix฀

  Solution ................................................................................................................................................ The฀Code...............................................................................................................................................

  3-7.฀Using฀Panorama฀and฀Pivot฀Controls ....................................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  3-8.฀Spicing฀Up฀the฀User฀Interface฀with฀the฀Silverlight฀Toolkit...................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  3-9.฀Using฀Launchers฀and฀Choosers.............................................................................. Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  3-10.฀Accessing฀to฀the฀Contacts฀Library........................................................................ Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage.................................................................................................................................................... x฀

  

3-11.฀Adding฀Advertising฀to฀Your฀Application................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

   ฀ Chapter฀4:฀User฀Interface฀with฀฀Expression฀Blend ..............................................

4-1.฀Getting฀Ready฀for฀Light฀and฀Dark ...........................................................................

  

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  

4-2.฀Changing฀the฀Skin฀of฀Your฀App...............................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

  

4-3.฀Creating฀Some฀Cool฀Animation...............................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  

4-4.฀Customizing฀a฀Control ............................................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

xi฀

  The฀Code...............................................................................................................................................  ฀ Chapter฀5:฀Gestures ............................................................................................

  5-1.฀Managing฀Gestures฀in฀a฀Silverlight฀Windows฀Phone฀Application ........................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  5-2.฀Adding฀Gestures฀Management฀to฀Click-less฀Silverlight฀Controls ........................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  5-3.฀Handling฀Gestures฀in฀a฀Graphical฀Context,฀Such฀as฀a฀Game฀Menu ........................ Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  5-4.฀Managing฀Gestures฀from฀the฀Silverlight฀for฀Windows฀Phone฀Toolkit..................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage.................................................................................................................................................... xii฀

   ฀ Chapter฀6:฀Sensors .............................................................................................

6-1.฀Resetting฀a฀Form฀by฀Shaking฀the฀Phone ................................................................

  

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  

6-2.฀Creating฀a฀Seismograph.........................................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  

6-3.฀Indicating฀the฀User’s฀Position฀on฀a฀Map.................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  

6-4.฀Displaying฀Sunset฀and฀Sunrise...............................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  

6-5.฀Indicating฀the฀User’s฀Position฀via฀Coordinates.......................................................

Problem ................................................................................................................................................

xiii฀

  How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  6-6.฀Using฀the฀Radio ...................................................................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  6-7.฀Building฀an฀Augmented฀Reality฀Application ........................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

   ฀ Chapter฀7:฀Media฀Management........................................................................... 7-1.฀Taking฀a฀Photo฀from฀Your฀Phone฀Camera ..............................................................

  Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  7-2.฀Capturing฀Photo฀Camera฀Video฀Frames.................................................................. Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage.................................................................................................................................................... xiv฀

  7-3.฀Picking฀a฀Photo฀from฀Your฀Media฀Library...............................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  7-4.฀Add฀Integration฀Between฀Your฀Application฀and฀Windows฀Phone ...........................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  7-5.฀Using฀Media฀Player฀to฀Shuffle฀Songs฀in฀Your฀Media฀Library ..................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  7-6.฀Playing฀Music฀Streamed฀from฀SkyDrive.................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  7-7.฀Using฀the฀Microphone฀in฀the฀Funny฀Repeater฀Application .....................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

xv฀

  The฀Code............................................................................................................................................... 7-8.฀Using฀the฀MediaElement฀Control฀to฀Play฀Both฀Music฀and฀Video ............................

  Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  7-9.฀Adding฀Integration฀with฀the฀Music-Videos฀Hub...................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

   ฀ Chapter฀8:฀Isolated฀Storage ................................................................................ 8-1.฀Saving฀and฀Loading฀a฀File฀in฀Isolated฀Storage .......................................................

  Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  8-2.฀Saving฀Serialized฀Data ........................................................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  8-3.฀Implementing฀the฀Local฀Database.......................................................................... xvi฀

  

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  8-4.฀Modifying฀the฀Settings฀of฀Your฀Application ............................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  8-5.฀Saving฀a฀Photo฀in฀Isolated฀Storage ........................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  8-6.฀Using฀Isolated฀Storage฀Explorer .............................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

Usage....................................................................................................................................................

  8-7.฀Implementing฀Background฀File฀Transfer................................................................

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................

xvii฀

  Usage....................................................................................................................................................  ฀ Chapter฀9:฀In฀the฀Cloud .......................................................................................

  9-1.฀Interacting฀with฀WCF.............................................................................................. Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  9-2.฀Accessing฀OData฀Services...................................................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  9-3.฀Creating฀a฀Feed฀Reader.......................................................................................... Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  9-4.฀Creating฀a฀Google฀API–based฀Translator................................................................ Problem ................................................................................................................................................ How฀It฀Works ........................................................................................................................................ Usage....................................................................................................................................................

  9-5.฀Pushing฀Your฀Notification....................................................................................... Problem ................................................................................................................................................ xviii฀

  

Solution ................................................................................................................................................

The฀Code...............................................................................................................................................

   ฀ Chapter฀10:฀Testing฀Windows฀Phone฀Applications .............................................

10-1.฀Implementing฀MVVM฀on฀Windows฀Phone฀Using฀MVVM฀Light...............................

  

Problem ................................................................................................................................................

How฀It฀Works ........................................................................................................................................