Table Of Contentwww.it-ebooks.info
Windows 8 App Projects
XAML and C# Edition
Nico Vermeir
www.it-ebooks.info
Windows 8 App Projects: XAML and C# Edition
Copyright © 2013 by Nico Vermeir
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material
supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the
purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the
Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from
Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are
liable to prosecution under the respective Copyright Law.
ISBN 978-1-4302-5065-4
ISBN 978-1-4302-5066-1 (eBook)
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.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning
Lead Editor: Jonathan Hassell
Developmental Editor: Kate Blackham
Technical Reviewer: Kevin Dockx
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan,
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: Katie Sullivan
Copy Editor: Mary Bearden
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit
www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science +
Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
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 web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to readers
at www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/.
www.it-ebooks.info
Contents at a Glance
About the Author .................................................................................................................xi
About the Technical Reviewer ...........................................................................................xiii
Acknowledgments ..............................................................................................................xv
(cid:78) Chapter 1: Introduction .....................................................................................................1
(cid:78) Chapter 2: Getting Started .................................................................................................5
(cid:78) Chapter 3: Building a First Project ..................................................................................21
(cid:78) Chapter 4: Consuming Data .............................................................................................43
(cid:78) Chapter 5: Build a Charming Application ........................................................................67
(cid:78) Chapter 6: Using Live Tiles ..............................................................................................97
(cid:78) Chapter 7: Notifications .................................................................................................119
(cid:78) Chapter 8: Building an Adaptable App ...........................................................................131
(cid:78) Chapter 9: Sensors ........................................................................................................151
(cid:78) Chapter 10: Games ........................................................................................................173
(cid:78) Chapter 11: The Windows Store ....................................................................................195
Index .................................................................................................................................205
iii
www.it-ebooks.info
Contents
About the Author .................................................................................................................xi
About the Technical Reviewer ...........................................................................................xiii
Acknowledgments ..............................................................................................................xv
(cid:78) Chapter 1: Introduction .....................................................................................................1
History ...........................................................................................................................................1
Windows Application Programming Interface ...............................................................................3
About This Book ............................................................................................................................4
(cid:78) Chapter 2: Getting Started .................................................................................................5
Setting Up the Environment ..........................................................................................................5
Starting a First Project ..................................................................................................................8
Class Library ..........................................................................................................................................................8
Windows Runtime Component ..............................................................................................................................8
Unit Test Library .....................................................................................................................................................9
Blank App Template ...............................................................................................................................................9
Grid App Template ................................................................................................................................................10
Split App Template ...............................................................................................................................................15
A Closer Look at the Simulator ....................................................................................................17
Summary .....................................................................................................................................20
(cid:78) Chapter 3: Building a First Project ..................................................................................21
Adding Pages ..............................................................................................................................21
GridView ..............................................................................................................................................................22
StackPanel ...........................................................................................................................................................25
v
www.it-ebooks.info
(cid:78) CONTENTS
Grid ......................................................................................................................................................................26
Canvas .................................................................................................................................................................27
Our Project ...........................................................................................................................................................27
Adding a Page ......................................................................................................................................................28
QuizPage ..............................................................................................................................................................31
MVVM and MVVM Light ...............................................................................................................32
Getting Started with MVVM Light .........................................................................................................................32
ViewModelLocator ...............................................................................................................................................34
ViewModels .........................................................................................................................................................36
Navigation in MVVM .............................................................................................................................................38
Where Are My Behaviors? ...........................................................................................................41
Summary .....................................................................................................................................42
(cid:78) Chapter 4: Consuming Data .............................................................................................43
SQLite in Windows 8 ....................................................................................................................44
Creating a Basic SQLite App ................................................................................................................................44
Adding the Child and Observation Classes ..........................................................................................................45
Building the Database and Its Connection ...........................................................................................................46
App Functionality .................................................................................................................................................47
Observation Page.................................................................................................................................................48
AppBar .................................................................................................................................................................49
Add Child Form ....................................................................................................................................................51
Displaying Data on MainPage ..............................................................................................................................52
Running the App ..................................................................................................................................................53
Consuming REST Services ..........................................................................................................53
Twitter Search App ..............................................................................................................................................53
The Tweet Class ...................................................................................................................................................53
Windows Azure Mobile Services .................................................................................................56
Creating a Mobile Service....................................................................................................................................56
Creating the Windows Store App .........................................................................................................................62
Summary .....................................................................................................................................65
vi
www.it-ebooks.info
(cid:78) CONTENTS
(cid:78) Chapter 5: Build a Charming Application ........................................................................67
Search Charm ..............................................................................................................................67
Filters ...................................................................................................................................................................71
Search Suggestions from a List ...........................................................................................................................75
Search Suggestions from Known Folders............................................................................................................77
Search Suggestions Through Open Search .........................................................................................................79
Share Charm ...............................................................................................................................82
Sharing Text .........................................................................................................................................................82
Sharing Links .......................................................................................................................................................84
Sharing HTML ......................................................................................................................................................86
Sharing Images ....................................................................................................................................................87
Sharing Custom Data ...........................................................................................................................................88
Settings Charm ............................................................................................................................91
Summary .....................................................................................................................................95
(cid:78) Chapter 6: Using Live Tiles ..............................................................................................97
Application Tile ............................................................................................................................97
Build Your Own Live Tile ......................................................................................................................................97
The Button Click ..................................................................................................................................................98
Live Tile with Image ...........................................................................................................................................101
Secondary Tiles .........................................................................................................................104
Adding a Secondary Tile ....................................................................................................................................104
Adding Navigation to the Secondary Tile ...........................................................................................................106
Updating a Secondary Tile .................................................................................................................................107
Badges ......................................................................................................................................109
Building the Badges...........................................................................................................................................110
Updating Tiles ............................................................................................................................112
Notification Queues ...........................................................................................................................................113
Background Tasks .............................................................................................................................................113
Summary ...................................................................................................................................117
vii
www.it-ebooks.info
(cid:78) CONTENTS
(cid:78) Chapter 7: Notifications .................................................................................................119
Toast Notifications .....................................................................................................................119
Building a Toast-Capable App ............................................................................................................................119
Toast Templates .................................................................................................................................................122
Toasts with Images ............................................................................................................................................123
Scheduling a Notification...................................................................................................................................124
Adding Audio to Toasts ......................................................................................................................................126
Lockscreen Notifications ...........................................................................................................127
Summary ...................................................................................................................................130
(cid:78) Chapter 8: Building an Adaptable App ...........................................................................131
Multiple Resolutions ..................................................................................................................131
Grids ..................................................................................................................................................................131
Testing Multiple Resolutions ..............................................................................................................................138
Dots Per Inch .....................................................................................................................................................139
GridView ............................................................................................................................................................140
Variable-Sized WrapGrid ....................................................................................................................................144
Changing the App Layout ..........................................................................................................144
Visual State Manager.........................................................................................................................................144
Snap That App ....................................................................................................................................................145
Device Orientation .............................................................................................................................................148
Summary ...................................................................................................................................149
(cid:78) Chapter 9: Sensors ........................................................................................................151
Accelerometer Sensor ...............................................................................................................151
How Does It Work? ............................................................................................................................................152
Using the Windows 8 Accelerometer API ...........................................................................................................152
Location Sensor .........................................................................................................................156
How Does It Work? ............................................................................................................................................156
Using the Windows 8 Location API.....................................................................................................................157
viii
www.it-ebooks.info
(cid:78) CONTENTS
Simple Orientation Sensor.........................................................................................................159
How Does It Work? ............................................................................................................................................160
Using the Windows 8 Simple Orientation API ....................................................................................................160
Light Sensor ..............................................................................................................................163
How Does It Work? ............................................................................................................................................163
Using the Windows 8 Light Sensor API ..............................................................................................................163
Inclinometer Sensor ..................................................................................................................165
How Does It Work? ............................................................................................................................................166
Using the Windows 8 Inclinometer API ..............................................................................................................166
Gyroscope Sensor .....................................................................................................................168
How Does It Work? ............................................................................................................................................168
Using the Windows 8 Gyroscope API .................................................................................................................168
Compass Sensor .......................................................................................................................170
How Does It Work? ............................................................................................................................................170
Using the Windows 8 Compass API ...................................................................................................................170
Summary ...................................................................................................................................171
(cid:78) Chapter 10: Games ........................................................................................................173
Direct3D ....................................................................................................................................173
Direct2D ....................................................................................................................................175
SharpDX ....................................................................................................................................178
MonoGame ................................................................................................................................178
Installing MonoGame .........................................................................................................................................179
Starting a MonoGame Project ...................................................................................................180
Game Class ........................................................................................................................................................182
Adding the Model ...............................................................................................................................................185
MouseManager class.........................................................................................................................................190
Game Logic ........................................................................................................................................................191
Summary ...................................................................................................................................193
ix
www.it-ebooks.info
(cid:78) CONTENTS
(cid:78) Chapter 11: The Windows Store ....................................................................................195
Creating a Developer Account ...................................................................................................195
Prepare to Submit Your App ......................................................................................................198
Submit Your App ........................................................................................................................201
Summary ...................................................................................................................................204
Index .................................................................................................................................205
x
www.it-ebooks.info
Description:Become a leading Windows 8 app developer by using Windows 8 App Projects - XAML and C# Edition to learn techniques, tools, and ideas to create successful, 5-star apps. Windows 8 App Projects - XAML and C# Edition shows you the nuts and bolts of the Windows 8 development ecosystem. Then, through a s