Table Of Contentwww.it-ebooks.info
www.it-ebooks.info
Programming Windows Store
Apps with C#
Matt Baxter-Reynolds and Iris Classon
www.it-ebooks.info
Programming Windows Store Apps with C#
by Matt Baxter-Reynolds and Iris Classon
Copyright © 2014 Matt Baxter-Reynolds. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Maria Stallone and Rachel Roumeliotis Indexer: Judith McConville
Production Editor: Melanie Yarbrough Cover Designer: Randy Comer
Copyeditor: Rachel Monaghan Interior Designer: David Futato
Proofreader: Charles Roumeliotis Illustrator: Rebecca Demarest
February 2014: First Edition
Revision History for the First Edition:
2014-02-10: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449320850 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Programming Windows Store Apps with C#, the image of a pika, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-32085-0
[LSI]
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Making the Transition from .NET (Part 1). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Why WinRT? 1
Philosophical Differences 2
Objectives 3
The New Project Templates 3
WinRT Metadata 4
Project Settings and Adding References 9
Building a Basic User Interface 11
UI Tracks 11
XAML Parsing Basics 14
Building a Basic Page 15
Implementing MVVM 25
WPF and Silverlight 26
MVVM Structure and Inversion of Control 28
Creating the View-Model and Running the App 38
2. Making the Transition from .NET (Part 2). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Inversion of Control 47
Installing TinyIoC 48
Initializing IoC Defaults 49
Understanding Asynchrony 53
How Asynchrony Works in WinRT 55
Calling the Server 60
Building the Service Proxies 60
Building the Register Method 63
Finishing the UI to Call the Register Server Function 67
Logon 68
iii
www.it-ebooks.info
Building LogonServiceProxy 69
Building the Logon Page 70
Busy Indicators 75
Positioning the Indicator 75
Showing the Indicator 77
3. Local Persistent Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
SQLite and sqlite-net 84
Working with SQLite 85
A Primer on Object-Relational Mapping 86
Using the Micro-ORM in sqlite-net 87
Storing Settings 89
The SettingItem Class 89
Linking in sqlite-net 91
Creating the Database Table for SettingItem 95
Reading and Writing Values 96
Modifying LogonPageViewModel 97
Caching Data Locally 100
Local Caching 100
Mapping JSON to Database Entities 101
Creating Test Reports 102
Setting Up the User Database 103
Creating ReportsPage 105
Using Templates 106
Building a Local Cache 109
Updating the Cache 112
Returning Reports from the Server 113
The Items Property 114
4. The App Bar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Adding a Simple App Bar 120
Getting Started with an App Bar 121
App Bar Behavior 124
App Bar with Single-Select Grid 126
App Bar with Multiselect Grid 126
A More Complex App Bar Implementation 127
Showing the App Bar on Multiple Selections 128
Checking Touch Operations 133
Showing the App Bar on Right-Click 135
Showing Context Options 136
App Bar Images 140
The Glyph Method 140
iv | Table of Contents
www.it-ebooks.info
Using Images 146
5. Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Local Notifications 156
Turning Notifications On and Off 156
XML Templates 156
Toast 158
Badges 167
Tiles 170
Other Notification Features 176
Push Notifications 177
WNS Process 177
Handling User Accounts 179
Obtaining a Notification URI 180
Sending to WNS 182
Troubleshooting Tips 191
6. Working with Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The File Picker 193
File Associations 196
Launching the App 197
Handling the Launch 199
Sandboxed File Access 201
Walking and Copying Pictures 203
Roaming Files 206
Multiple Devices 207
Setting Up the Remote Debugging Client 207
Syncing Files 209
Roaming Settings 210
Using Files with StreetFoo 210
Getting Report Images 211
Migrating to ReportViewItem 212
Implementing ReportImageCacheManager 217
7. Sharing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Sharing Data 224
Basic Sharing 224
Pull Requests/Deferrals 233
Acting as a Share Target 235
Sharing Text 235
Sharing Text (and Troubleshooting) 238
Long-Running Operations 249
Table of Contents | v
www.it-ebooks.info
Sharing Images 251
Quick Links 254
8. Searching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Implementing Search 258
Creating the Search Results Page 258
Creating SearchResultsPageViewModel 258
Implementing the Search Operation 263
Refining Search 276
Placeholder Text 277
Suggestions 278
Remembering Where We Were 284
Using the SearchBox 289
Other Best-Practice Notes 291
9. Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Adding Options 293
Standard Options 294
Adding Custom Options 294
Implementing the Settings Flyout 297
Building a Settings Pane 297
Building MySettingsFlyout 301
Developing a Help Screen 303
Creating a Help Pane 303
Handling the F1 Key 305
Rendering Markup 306
10. Location. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Creating a Singleton View 311
Creating the View-Model 312
Creating the View 315
Navigating to the View 318
Retrieving a Current Location 322
Using the Simulator with Location 327
Integrating Maps 328
Adding the Bing Maps Control 329
Handling Input with the View 331
Packaging Points for Display 332
Showing Points on the Map 336
Shelling to the Maps App 339
11. Using the Camera. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
vi | Table of Contents
www.it-ebooks.info
Capturing Photos 344
Creating EditReportPage 345
Building EditReportPageViewModel and Its View-Model 346
Saving and Canceling 352
Adding the New Option 355
Handling Temporary Files 356
Changing the Manifest 356
Taking Pictures 357
Implementing Save 360
Validating and Saving 360
Resizing Images 363
12. Responsive Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Updating the Grid View 371
The VisualStateManager 371
Creating MyListView 373
Modifying the App Bar 375
Updating Singleton Views 377
Adding a More Button to the App Bar 380
Handling Views That Don’t Support 320-Pixel Width 385
13. Resources and Localization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.pri Files 387
Adding Strings 390
Localizing Strings 393
Default Project Locales 393
Localizing Strings in XAML 394
Conventions 398
Changing Other Properties 399
Explicitly Loading Strings 399
Localizing Images 402
Varying Images by Locale 402
Varying Images by Display DPI 405
14. Background Tasks and App Lifetime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
App Lifetime 410
Background Tasks API 411
CPU Usage Quota 412
Triggers and Conditions 413
Execution Model 415
Implementing a Sync Background Task 416
Building the Façade 422
Table of Contents | vii
www.it-ebooks.info
Debugging the Task 425
Troubleshooting Background Tasks 427
Restricting the Run Period 428
Implementing the Sync Function 433
Sending Changes 434
Receiving New Work 438
Signaling the App from the Background Task 443
Putting the App on the Lock Screen 444
15. Sideloading and Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Using the Windows App Certification Kit 450
Distribution Through Production Sideloading 451
Turning on Sideloading on Windows 8 452
Installing Apps 453
Distribution Through the Windows Store 453
A. Cryptography and Hashing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
B. Unit Testing Basics for Windows Store Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
viii | Table of Contents
www.it-ebooks.info