Preface

The computing industry is changing. PC sales are on the decline, and sales of post-PC devices (tablets and smartphones) are on the ascendancy. This change can be understood easily enough—computers are no longer something used for work, they are something used for life, and happily there is more to our society than just work.

The massive commercial success of post-PC devices suggests that this change works OK for most, but for companies like Microsoft it creates a big problem. The PC is not going to be as important over the next 20 years as it has been for the last 20 years. Windows 8.1 and Windows RT are Microsoft’s first move to try to address that problem by making the Windows operating system “play more nicely” in the tablet space.

Microsoft has done this by introducing a new user interface paradigm called Modern UI. This new user interface paradigm is monochronistic (one thing at a time), rather than the polychronistic (many things at a time) nature of a normal windowing operating system. It is also optimized for touch.

As well as providing a new user interface, Microsoft has introduced a new API, called Windows Runtime (WinRT), and a new execution and packaging model for the apps, called Windows Store apps. We’ll talk more about the actual construction of Windows Store apps in Chapter 2.

This book is designed to treat Windows 8.1 and Windows 8.1.1 RT equally—nothing we do in the book will exclude operation from either variant of the operating system. Similarly, everything we do can be used in apps that are distributed through the Windows Store.

Generally, we will be writing all the code ourselves, but from time to time we will be using third-party products. Virtually all of these are open source—there is only one exception, which is the Bing Maps component discussed in Chapter 11. Everything else is unrestricted.

So let’s go! We’ll start by learning about the app that we’re going to build.

Audience

What I’ve tried to do with writing this book is to tell a story that takes you, the reader, through the process of moving from .NET development over to Windows Store app development. There is a slight bias in the book in that I’m assuming most developers have day jobs developing web applications and have been asked to look into development tablet apps that run on Microsoft’s tablet operating systems.

Some of you will also have done quite a bit of desktop development on Windows, particularly using Silverlight and/or Windows Presentation Foundation (WPF). This book isn’t a primer on developing XAML, although you will see and work with enough examples that use XAML to become proficient.

I’ll also tell you a bit more about the app we’ll be discussing so you can judge if this is the right book for you. The app has within it the common sorts of functionality that you find in line-of-business apps (LOB) generally. At the time of writing, the Windows tablet story is not established, so the elements that we’ll go through are those that apply to real applications on Windows Mobile, Android, and iOS that I’ve built over the past 10 years or so.

Although the application that we’ll build is a LOB app, everything you’ll see and do in this book applies equally well to a normal retail app that you might sell in a business-to-consumer fashion.

The Application

As I mentioned, we’re going to build a line-of-business app, rather than a “retail” app. The way that I distinguish between these two is that in a retail app, the software vendor typically doesn’t have a strong relationship with the end customer. In retail, the end customer finds the app through indirect recommendations and/or through the app store catalog. In a LOB app, proactive marketing and relationship-building activities look to tie a client and vendor together through some sort of commercial offering. Technically, however, there isn’t a big difference between retail apps and LOB apps.

The specific example I’m going to show you is a “field service” app. This type of app is a classic mobile working application. In field service you have a number of operatives whose operational control is within your remit. You send them out into the field to do something—either something specific (“go here, fix this”), or something reactive (e.g., someone “patrols” an area and reports back on problems).

The app we will build will be called StreetFoo, and it’s a blend of those last two examples. I have created a simple server that is hosted on AppHarbor that will serve as the backend service for the app. When the user logs in to the app, it will download a set of “problem reports.” Each report will be something that needs fixing—the sample data happens to show graffiti, but it could be anything. The concept of the app is that the user would then either fix the problem or could report new problems into the app. Updates to problems or new problems are then updated to the server.

That’s the basic functionality. There are additional things that we’ll look at, such as capturing photos and location information, as well as all of the various special user experience features in Windows 8.1/Windows 8.1.1 RT—sharing, snapped view, search, and so on.

The Chapters

We’ll start in Chapters 1 and 2 with a primer designed to get you up and running in terms of moving from .NET into this new world.

Chapter 1, Making the Transition from .NET (Part 1)

Starts by explaining the “break” between .NET and associated technologies (specifically WPF) over to Windows Runtime (WinRT). You’ll then build a basic user interface and implement a Model/View/View-Model (MVVM) pattern.

Chapter 2, Making the Transition from .NET (Part 2)

Walks you through making the UI you built in Chapter 1 do something—specifically, calling up to the server to register a new user account. This chapter also has a detailed look at asynchrony—probably the most important thing that you will learn during your time with Windows Store app development.

The remaining chapters in this book each focus on a specific API feature area.

Chapter 3, Local Persistent Data

Explores SQLite. The reason I’ve brought up this topic so early in the book is that you can’t build a practically useful application without having some sort of persistent store. Although you can store information on disk easily enough using the Windows Store APIs, SQLite is the de facto relational database used in mobile solutions, and so we’ll use it in our app.

Chapter 4, The App Bar

Introduces the first of the special Windows 8.1 user experience (UX) features: the app bar. App bars are the small panels that pop in from the top and bottom of the screen and provide access to options and tabs. (The app bar is essentially analogous to toolbars.) We’ll look at how to build an app bar and how to make up our own images for use on the buttons.

Chapter 5, Notifications

Discusses notifications. Notifications in Windows Store apps can be used to update the tile on the Start screen, add badges to the tile, and display toast (the notifications that wind in from the top-right side of the screen). Notifications can be created locally and shown locally, or alternatively, created on the server and pushed out to all connected devices using Windows Push Notification Services (WNS). In this chapter we’ll look at both routes.

Chapter 6, Working with Files

Looks in detail at working with files. To be honest, when I first planned this book I didn’t intend to include a chapter on files, as this tends to be a topic well served by the community whenever a new platform is introduced. However, I ended up adding this chapter to handle images. Each report that we track in the app will have exactly one image. Rather than storing these images in SQLite, which is impractical, we’ll store them on disk.

Chapter 7, Sharing

Focuses on the Windows 8.1 sharing feature. Sharing is one of the key differentiators between Microsoft’s tablet strategy and other platforms. Most platforms “silo off” apps and make it hard to share data. Windows 8.1 has a declarative model for sharing where apps indicate they can serve up certain types of data. That data can then be read in by another app that supports consumption of shared data. In this chapter we’ll look at both sharing data from our app and consuming data from other apps.

Chapter 8, Searching

Looks at the Windows 8.1 UX feature of searching. The idea here is that, generally, all apps need some sort of search feature. In Windows 8.1 this is accessed from the charms or by using the SearchBox control. In this chapter we’ll look at implementing a search feature that we can use to find problem reports.

Chapter 9, Settings

Concludes our look at Windows 8.1 UX specifics with a discussion of the settings charm, which—as its name implies—provides a common area where developers can put settings. It’s also a common place to put links up for support information and privacy policies. In this chapter we’ll go a little broad with this by using the SettingsFlyout control to load and render Markdown-formatted text.

Chapter 10, Location

Explores location, a very common requirement for mobile LOB apps because it’s often helpful to have some “evidence” of where a particular activity took place, or to utilize the user’s location as a way of creating new data. In this chapter we’ll look at the basics of reading locational information from the device, and we’ll also use the Bing Maps Windows Store apps control to present a map within the application.

Chapter 11, Using the Camera

Helps you discover how to use the camera. In mobile LOB apps it’s often a requirement to gather photographic evidence of work done. (For example, if someone is asked to fix a sink, you may find it helpful to have a photo of the sink before and after it was fixed.) In this chapter we’ll look at how to create new problem reports, starting with a photograph taken from the webcam.

Chapter 12, Responsive Design

Helps you master how to implement responsive design so the application can be resized in width to support even the smallest width size of 320 pixels, previously known as “snapped mode.” Another Windows 8.1 UX feature that differentiates Windows from the other platforms is the ability to run apps side by side. The way this works is that you can have one app running in a thin strip on the left or right side of the screen, with another app taking up the remainder of the space. The only problem with this is that you need to build an entirely parallel UI to get your app running in this thin strip. In fact, this isn’t as bad as it sounds, because the MVVM pattern that we’ll use abstracts a lot of the work away. Specifically, in this chapter we’ll build in the ability to run our application in different width sizes.

Chapter 13, Resources and Localization

Looks at resources and localization. By the time you get to this chapter, you will already have seen quite a few ways of working with resources, so some of this chapter is given over to covering the things that we haven’t yet looked at in detail. In the other part of the chapter, we’ll discuss how to implement proper localization of the app (i.e., how to add in support to present the app in different languages).

Chapter 14, Background Tasks and App Lifetime

Tackles background tasks, a special way of blocking off functionality that Windows will run on a schedule on the application’s behalf. Common to all tablet platforms, Windows Store apps look to restrict what your application can do when it’s not actually running in the foreground. In this chapter we’ll look in some detail at implementing such background tasks—specifically, we’re going to look at how to use this functionality to download new reports and upload local change reports back to the server in the background.

Chapter 15, Sideloading and Distribution

Details how you can actually package and distribute apps on the Windows Store. We’ll look at using developer licenses to create sideloading packages for internal testing, and we’ll also look at how to do proper enterprise sideloading. (Sideloading is the process whereby you distribute apps to a private audience rather than using the Windows Store.) We’ll also look at the rules that you need to adhere to in order to get Microsoft to distribute your app on the Windows Store.

The book has two appendixes:

Appendix A

Covers some common requirements related to cryptography and hashing that you’ll likely either need or be asked about, but don’t fit into the main body of the text.

Appendix B

Looks at how to unit test your code using the unit testing projects provided with Visual Studio. This will use the inversion of control containers that we built and used throughout the book.

And that’s it! By the time you’ve been through the whole story, you should have a great understanding of how to build full-featured Windows Store applications.

Prerequisites

The only thing that you will need in order to get working is Visual Studio 2013. Out of the box, this edition of Visual Studio comes with everything you need to build Windows Store apps. You can use either the Professional edition or the Express edition. I happened to use the Professional edition, but everything has been tested on Express.

You will need a Windows Store developer account if you want to actually get your apps listed on the store, although nothing in this book requires that level of paid account. You will need to create a free account in order to obtain a developer license, which is required to locally deploy any apps that you build.

Source Code

The source code for this book is available on GitHub.

The easiest way to work with this code is to grab the entire repo and put it on your local disk. Each chapter is represented by one folder, where the code in the folder is the same as the state of the application at the end of each chapter. (For example, the Chapter8 folder includes everything from Chapter 2 up to and including Chapter 8. The Chapter9 folder builds on the Chapter8 folder and also includes the work that we go through in Chapter 9.) From time to time, the code downloads may contain more than what we have specifically gone through in the book.

If you’re not accustomed to using GitHub or git, here’s a quick run-through.

Using git

This section is intended to get you through the basics of installing git and using it to fetch the code from the repository. It’s not intended to show you how to use git as a source control system, but GitHub offers a decent walkthrough of that.

You’ll need a git client to get started. You can download a client from the git website. Do this and install the package that’s downloaded.

The installer will install both a command-line client and a GUI. Personally, when I’m actually using git I always use the command line. A lot of people use the GUI. If you’re only interested in using git to get the code for this book, you might as well just use the GUI.

To open the GUI in Windows 8.1, access the Start screen by pressing the Windows key. Type git directly into the Start screen. You’ll get options for Git Bash and Git GUI. Open up the Git GUI and select the Clone Existing Repository option.

You’ll need to copy and paste the path of the repo from GitHub. To do this, access the repo using a web browser. The URL you want is https://github.com/mbrit/ProgrammingWindowsStoreApps.

On the page you’ll find a “Quick setup” box that contains the actual URL of the repo. You’ll need to copy this to the clipboard. The URL will look something like https://github.com/mbrit/ProgrammingWindowsStoreApps.git. Figure 1 illustrates.

The area of the GitHub page showing the actual repo URL
Figure 1. The area of the GitHub page showing the actual repo URL

Back in the GUI, copy and paste the repo path into the Source Location field, and type the path to any local folder that you like into the Target Directory field. Figure 2 illustrates.

Setting up the clone operation
Figure 2. Setting up the clone operation

Click the Clone button, and the repo will come down to your local machine. You can then use Visual Studio to open the solution files contained in each folder.

Contacting the Authors

Should you want to get hold of Matt directly, the best way is via Twitter (@mbrit). Alternatively, try his website. Iris Classon can be contacted via Twitter (@irisclasson) and her website.

Let’s Go!

And that’s it. You should now be ready to get going building Windows Store apps.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl).

Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values.

Note

This icon signifies a general note.

Tip

This icon signifies a tip or suggestion.

Warning

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Programming Windows Store Apps with C# by Matt Baxter-Reynolds and Iris Classon (O’Reilly). Copyright 2014 Matthew Baxter-Reynolds, 978-1-449-32085-0.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/prog-win-store-apps-csharp.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Thank you to the technical reviewers, Oren Novotny, Stefan Turalski, Matt Fitchett, and Nathan Jepson, without whom this book would not have been possible.

This book would be fundamentally different, and nowhere near as good or complete, were it not for Twitter. Twitter is perhaps the most important learning resource for those involved in the computer industry that’s ever been invented. This book has got quite a lot of advice in it, and the most important piece is this: if you’re a professional software developer and you don’t use Twitter, start.

Here’s a list of my various Twitter friends who have given support, saved me hours upon hours of work, come up with new ideas, and provided invaluable input:

  • Alex Papadimoulis (@apapadimoulis)

  • Casey Muratori (@cmuratori)

  • Chris Field (@mrcfield)

  • Chris Hardy (@chrisntr )

  • Craig Murphy (@camurphy)

  • Daniel Plaisted (@dsplaisted)

  • David Kean (@davkean)

  • Duncan Smart (@duncansmart)

  • Edward Behan (@edwardbehan)

  • Filip Skakun (@xyzzer)

  • Frank Krueger (@praeclarum)

  • Gill Cleeren (@gillcleeren)

  • Ginny Caughey (@gcaughey)

  • Haris Custo (@hariscusto)

  • Hermit Dave (@hermitdave)

  • Iris Classon (@irisclasson)

  • Jamie Mutton (@jcmm33)

  • Joel Hammond-Turner (@rammesses)

  • Jose Fajardo (@josefajardo)

  • Keith Patton (@kpatton)

  • Kendall Miller (@kendallmiller)

  • Liam Westley (@westleyl)

  • Mark Tepper (@binaerforceone)

  • Matt Hidinger (@matthidinger)

  • Matthieu GD (@tewmgd)

  • Mike Harper (@mikejharper)

  • Nic Wise (@fastchiken)

  • Peter Provost (@pprovost)

  • Ross Dargan (@rossdargan)

  • Tim Heuer (@timheuer)

  • Tomas McGuinness (@tomasmcguinness)

Finally, thank you to Rachel Roumeliotis, Maria Gulick, Melanie Yarbrough, and the rest of the O’Reilly team for their hard work and patience in making this book a reality.

Get Programming Windows Store Apps with C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.