Tải bản đầy đủ (.pdf) (220 trang)

Windows 8 App Projects XAML and C# Edition pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.43 MB, 220 trang )

www.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
is work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microlms 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 specically 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 benet of the trademark owner, with no intention of infringement of the trademark.
e use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identied
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. e 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, Je Olson, Jerey 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 , 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 , 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
iii
Contents at a Glance
About the Author xi
About the Technical Reviewer xiii
Acknowledgments xv
Chapter 1: Introduction N 1
Chapter 2: Getting Started N 5
Chapter 3: Building a First Project N 21

Chapter 4: Consuming Data N 43
Chapter 5: Build a Charming Application N 67
Chapter 6: Using Live Tiles N 97
Chapter 7: Notifications N 119
Chapter 8: Building an Adaptable App N 131
Chapter 9: Sensors N 151
Chapter 10: Games N 173
Chapter 11: The Windows Store N 195
Index 205
www.it-ebooks.info
v
Contents
About the Author xi
About the Technical Reviewer xiii
Acknowledgments xv
NChapter 1: Introduction 1
History 1
Windows Application Programming Interface 3
About This Book 4
Chapter 2: Getting Started N 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

Chapter 3: Building a First Project N 21
Adding Pages 21
GridView 22
StackPanel 25
www.it-ebooks.info
N CONTENTS
vi
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
Chapter 4: Consuming Data N 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
www.it-ebooks.info
N CONTENTS
vii
Chapter 5: Build a Charming Application N 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
Chapter 6: Using Live Tiles N 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
www.it-ebooks.info
N CONTENTS
viii
Chapter 7: Notifications N 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
Chapter 8: Building an Adaptable App N 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
Chapter 9: Sensors N 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
www.it-ebooks.info
N CONTENTS
ix
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
Chapter 10: Games N 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
www.it-ebooks.info
N CONTENTS
x
Chapter 11: The Windows Store N 195
Creating a Developer Account 195
Prepare to Submit Your App 198
Submit Your App 201
Summary 204
Index 205
www.it-ebooks.info
xi
About the Author
Nico Vermeir is an MCP living in Belgium. He is currently employed as a NET
mobile developer in the Mobile Solution Center at RealDolmen, one of Belgium’s
leading IT single source providers, where he focuses on developing apps for
Windows Phone and Windows 8. He is also a founding member and board member
of the Belgian Metro App Developer Network, a user group focused on Windows 8
and Windows Phone development. Since June 2012 he has been a proud member of
Microsoft’s Extended Experts Team Belgium. If you’re in Belgium, feel free to drop

by an event: .
www.it-ebooks.info
xiii
About the Technical Reviewer
Kevin Dockx lives in Belgium and works at RealDolmen, one of Belgium’s biggest
ICT companies. He is a 31-year-old technical specialist/project leader on NET
(web) applications, mainly XAML based, and a solution owner for Windows 8 Store
applications. His main focus lies in all things XAML, but he still keeps an eye on the
new developments concerning other products from the Microsoft NET (Web) Stack.
As an XAML enthusiast, he’s a regular speaker at various national and international
events, such as Microsoft Techdays in Belgium, Portugal, and Finland and
VISUG and MADN events, Simplicity Day, Community Day, among others.
He also writes articles for various XAML-related sites, and he wrote a best-selling
Silverlight book, Packt Publishing’s Silverlight 4 Data and Services Cookbook
( />cookbook/book) and its follow-up, the Silverlight 5 Data and Services Cookbook
(www.packtpub.com/microsoft-silverlight-5-data-and-services-cookbook/book). His blog, which contains
various tidbits on XAML, NET, and occasional ramblings, can be found at and you
can contact him on Twitter via @KevinDockx.
www.it-ebooks.info
xv
Acknowledgments
I would like to thank my wife, Kim, for letting me chase my dreams, no matter how crazy or how many lonely evenings
it meant for her.
I would also like to thank Kevin Dockx for being my technical reviewer and always being there to help or give
advice. I already learned a great deal from him and I sincerely hope we’ll be working together for many years to come.
And last but not least I would like to thank the Belgian Microsoft developers evangelist team for helping me with
test devices, advice, awesome events, and for supporting our great Belgian communities.
www.it-ebooks.info

×