Windows Runtime Via C#

3y ago
72 Views
3 Downloads
1.21 MB
70 Pages
Last View : 6d ago
Last Download : 3m ago
Upload by : Anton Mixon
Transcription

Delve inside the Windows Runtime—and learn best ways todesign and build Windows Store apps. Guided by Jeffrey Richter,a well-known expert in Windows and .NET programming, alongwith principal Windows consultant Maarten van de Bospoort,you’ll master essential concepts. And you’ll gain practical insightsand tips for how to architect, design, optimize, and debugyour apps.With this book, you will: Learn how to consume Windows Runtime APIs from C# Understand the principles of architecting Windows Store apps See how to build, deploy, and secure app packages Understand how apps are activated and the process model controlling their executionStudy the rich features available when working with files andfoldersExplore how to transfer, compress, and encrypt data via streamsDesign apps that give the illusion of running using live tiles,background transfers, and background tasksShare data between apps using the clipboard and the Share charmGet advice for monetizing your apps through the Windows StoreGet Visual C# code samplesDownload rpAbout this Book Requires working knowledge ofMicrosoft .NET Framework, C#, andthe Visual Studio IDE Targeted to programmers buildingWindows Store apps Some chapters also useful to thosebuilding desktop appsTechnologies Covered Windows 8.1 Microsoft Visual Studio 2013About the AuthorsJeffrey Richter is a cofounder ofWintellect (www.wintellect.com), atraining and consulting firm dedicatedto helping companies build bettersoftware faster. He is author of theclassic CLR via C#, now in its fourthedition, Windows via C/C , FifthEdition, and other popular .NETprogramming books and courses.Maarten van de Bospoort is a principalconsultant with the Premier Services forDevelopers division in Microsoft. In thisrole, he teaches professional developersworldwide how to write Windows appsand helps independent software vendorsarchitect and optimize their code forWindows.RichterVan deBospoortmicrosoft.com/mspressISBN: 978-0-7356-7927-6U.S.A. 34.99Canada 36.99[Recommended]Programming/WindowsCelebrating 30 years!WindowsRuntimevia C#ProfessionalExpert guidance for building Windows Store appswith the Windows RuntimeWindows Runtime via C#Windows Runtime via C#Jeffrey RichterMaarten van de Bospoort

Windows Runtime via C# Jeffrey RichterMaarten van de BospoortA01T679276.indd i10/29/2013 11:50:43 AM

PUBLISHED BYMicrosoft PressA Division of Microsoft CorporationOne Microsoft WayRedmond, Washington 98052-6399Copyright 2013 by Jeffrey RichterAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by anymeans without the written permission of the publisher.Library of Congress Control Number: 2013952561ISBN: 978-0-7356-7927-6Printed and bound in the United States of America.Second Printing: June 2014Microsoft Press books are available through booksellers and distributors worldwide. If you need support relatedto this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think ofthis book at soft and the trademarks listed at ctualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property oftheir respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, andevents depicted herein are fictitious. No association with any real company, organization, product, domain name,email address, logo, person, place, or event is intended or should be inferred.This book expresses the authors’ views and opinions. The information contained in this book is provided withoutany express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers ordistributors will be held liable for any damages caused or alleged to be caused either directly or indirectly bythis book.Acquisitions Editor: Devon MusgraveDevelopmental Editor: Devon MusgraveProject Editor: Carol DillinghamEditorial Production: Curtis Philips, Publishing.comTechnical Reviewer: Christophe Nasarre; Technical Review services provided byContent Master, a member of CM Group, Ltd.Copyeditor: Roger LeBlancIndexer: Lucie HaskinsCover: Twist Creative Seattle and Joel PanchotA02L679276.indd ii10/21/2013 5:16:04 PM

Kristin, words cannot express how I feel about our life together.I cherish our family and all our adventures. I’m filled each daywith love for you.Aidan (age 10) and Grant (age 5), you both have been aninspiration to me and have taught me to play and have fun.Watching the two of you grow up has been so rewarding andenjoyable for me. I am lucky to be able to partake in your lives.I love and appreciate you more than you could ever know.—JEFFREY RICHTERJeff takes a break while his family computes.A03D679276.indd iii11/1/2013 9:11:40 AM

To Jules and Joris. You guys have taught me so much. The twoof you have been inspirational, each in your own particular way.To Brigitte. For your tireless optimism, energy, love, andunwavering support.—MAARTEN VAN DE BOSPOORTMaarten and family celebrate the publication of his first book.A03D679276.indd iv11/1/2013 9:11:50 AM

Contents at a glanceA04G679276.indd vForewordxiiiIntroductionxviiPART ICORE CONCEPTSCHAPTER 1Windows Runtime primerCHAPTER 2App packaging and deployment25CHAPTER 3Process model49PART IICORE WINDOWS FACILITIESCHAPTER 4Package data and roaming79CHAPTER 5Storage files and folders91CHAPTER 6Stream input and output119CHAPTER 7Networking145CHAPTER 8Tile and toast notifications183CHAPTER 9Background tasks205CHAPTER 10Sharing data between apps229CHAPTER 11Windows Store247Appendix: App containers271Index275310/21/2013 5:22:00 PM

A04G679276.indd vi10/21/2013 5:22:05 PM

ContentsForeword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiWho should read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviiWho should not read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiiOrganization of this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiiCode samples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixErrata & book support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxWe want to hear from you. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiStay in touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPART ICORE CONCEPTSChapter 1Windows Runtime primer3Windows Store app technology stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6The Windows Runtime type system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Windows Runtime type-system projections . . . . . . . . . . . . . . . . . . . . . . . . . 11Calling asynchronous WinRT APIs from .NET code. . . . . . . . . . . . . . . . . . . . 16Simplifying the calling of asynchronous methods . . . . . . . . . . . . . . . 18Cancellation and progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19WinRT deferrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can improve our books and learning resourcesfor you. To participate in a brief survey, please visit:http://aka.ms/tellpressviiA05T679276.indd vii10/31/2013 1:56:41 PM

Chapter 2App packaging and deployment25A Windows Store app’s project files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25The app’s package manifest file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27Package identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31App (not package) declarations (extensions/contracts) . . . . . . . . . . 32Building a Windows Store app package . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34Contents of an .appx package file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Creating a bundle package file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Deploying a Windows Store package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40Restricted deployments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40Enterprise deployments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Windows Store deployments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Package staging and registration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44Wintellect’s Package Explorer desktop app . . . . . . . . . . . . . . . . . . . . . . . . . . 45Debugging Windows Store apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46Chapter 3Process model49App activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Managing the process model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55XAML page navigation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Process lifetime management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64Windows Store app suspension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Windows Store app termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66How to best structure your app class’ code . . . . . . . . . . . . . . . . . . . . 70Debugging process lifetime management . . . . . . . . . . . . . . . . . . . . . 75viiiContentsA05T679276.indd viii10/31/2013 1:56:45 PM

PART IICORE WINDOWS FACILITIESChapter 4Package data and roaming79Package data settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Package data storage folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83Versioning package data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83Roaming package data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85Package data change notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89Chapter 5Storage files and folders91The WinRT storage object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Package and user files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Accessing read-only package files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94Accessing read-write package files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95Accessing user files via explicit user consent . . . . . . . . . . . . . . . . . . . . . . . . . 97File-type associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101Storage item properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107Accessing user files with implicit user consent . . . . . . . . . . . . . . . . . . . . . .109Performing file and folder queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116Chapter 6Stream input and output119Simple file I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119The streams object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120Interoperating between WinRT streams and .NET streams . . . . . . . . . . . .123Transferring byte buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124Writing and reading primitive data types . . . . . . . . . . . . . . . . . . . . . . . . . .127Performing transacted write operations . . . . . . . . . . . . . . . . . . . . . . . . . . . .130Polite reader data access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131Compressing and decompressing data. . . . . . . . . . . . . . . . . . . . . . . . . . . . .134ContentsA05T679276.indd ixix10/31/2013 1:56:45 PM

Encrypting and decrypting data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136Populating a stream on demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138Searching over a stream’s content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140Chapter 7Networking145Network information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145Network isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147Network connection profile information . . . . . . . . . . . . . . . . . . . . . . . . . . .150How your app must use connectivity profile information . . . . . . .152Network connectivity change notifications. . . . . . . . . . . . . . . . . . . .153Background transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154Debugging background transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . .160HttpClient: Client-side HTTP(S) communication . . . . . . . . . . . . . . . . . . . . .161HttpBaseProtocolFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164Windows Runtime sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168Socket addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169StreamSocket: Client-side TCP communication . . . . . . . . . . . . . . . . 170StreamSocketListener: Server-side TCP communication . . . . . . . . .172StreamWebSocket: Streaming client-side WebSocketcommunication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173MessageWebSocket: Messaging client-side WebSocketcommunication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176DatagramSocket: Peer-to-peer UDP communication . . . . . . . . . . .177DatagramSocket: Multicast UDP communication . . . . . . . . . . . . . .180Encrypting data traversing the network with certificates . . . . . . . . . . . . .181Chapter 8Tile and toast notifications183Tiles and badges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184Updating a tile when your app is in the foreground . . . . . . . . . . . .186Placing a badge on a tile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188Animating a tile’s contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190Updating a tile at a scheduled time . . . . . . . . . . . . . . . . . . . . . . . . . .192Updating a tile periodically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192Secondary tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192xA05T679276.indd xContents10/31/2013 1:56:45 PM

Toast notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194Showing a toast notification at a scheduled time . . . . . . . . . . . . . .198Using the Wintellect Notification Extension Library . . . . . . . . . . . .199Windows Push Notification Service (WNS) . . . . . . . . . . . . . . . . . . . .199Chapter 9Background tasks205Background task architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205Step 1: Implement your background task’s code . . . . . . . . . . . . . . . . . . . .207Step 2: Decide what triggers your background task’s code . . . . . . . . . . .208Maintenance and time triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209System triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209Location triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210Push notification triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211Control channel triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212Step 3: Add manifest declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213Lock-screen apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214Step 4: Register your app’s background tasks . . . . . . . . . . . . . . . . . . . . . . .219Debugging background tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222Background task resource quotas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223Deploying a new version of your app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225Background task progress and completion . . . . . . . . . . . . . . . . . . . . . . . . .225Background task cancellation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227Chapter 10 Sharing data between apps229Apps transfer data via a DataPackage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229Sharing via the clipboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231Sharing via the Share charm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234Implementing a share source app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237Delayed rendering of shared content . . . . . . . . . . . . . . . . . . . . . . . .239Implementing a share target app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240Implementing an extended (lengthy) share operation . . . . . . . . . .244Share target app quick links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244Debugging share target apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245ContentsA05T679276.indd xixi10/31/2013 1:56:45 PM

Chapter 11 Windows Store247Submitting a Windows Store app to the Windows Store. . . . . . . . . . . . . .248Submitting your app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249Testing your app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252Monitoring your app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254Updating your app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255The Windows Store commerce engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256The Windows Store commerce engine WinRT APIs . . . . . . . . . . . . .257App trials and purchasing an app license . . . . . . . . . . . . . . . . . . . . .262Purchasing durable in-app product licenses . . . . . . . . . . . . . . . . . . .264Purchasing consumable in-app products . . . . . . . . . . . . . . . . . . . . .266Purchasing consumable in-app product offers . . . . . . . . . . . . . . . .269Appendix: App containers271Index275What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can improve our books and learning resourcesfor you. To participate in a brief survey, please .indd xii10/31/2013 1:56:45 PM

ForewordNo kidding! Take your seats, everyone, so we can get started. If you haven’t a cluewha

Expert guidance for building Windows Store apps with the Windows Runtime Delve inside the Windows Runtime—and learn best ways to design and build Windows Store apps. Guided by Jeffrey Richter, a well-known expert in Windows and .NET programming, along with principal Windows consultant Maarten van de Bospoort, you’ll master essential concepts.

Related Documents:

The Windows The Windows Universe Universe Windows 3.1 Windows for Workgroups Windows 95 Windows 98 Windows 2000 1990 Today Business Consumer Windows Me Windows NT 3.51 Windows NT 4 Windows XP Pro/Home. 8 Windows XP Flavors Windows XP Professional Windows XP Home Windows 2003 Server

AutoCAD 2000 HDI 1.x.x Windows 95, 98, Me Windows NT4 Windows 2000 AutoCAD 2000i HDI 2.x.x Windows 95, 98, Me Windows NT4 Windows 2000 AutoCAD 2002 HDI 3.x.x Windows 98, Me Windows NT4 Windows 2000 Windows XP (with Autodesk update) AutoCAD 2004 HDI 4.x.x Windows NT4 Windows 2000 Windows XP AutoCAD 2005 HDI 5.x.x Windows 2000 Windows XP

Oracle Container Runtime for Docker 19.03 1-2 Oracle Container Runtime for Docker 18.09 1-3 Oracle Container Runtime for Docker 18.03 1-3 Oracle Container Runtime for Docker 17.06 1-4 Docker 17.03 1-5 Docker 1.12 1-6 2 Installing Oracle Container Runtime for Docker Setting Up the Unbreakable Enterprise Kernel 2-1

Windows 10 IoT Enterprise. 1. A Universal Windows app is a Windows experience that is built upon the Universal Windows Platform (UWP), which was first introduced in Windows 8 as the Windows Runtime. Universal Windows apps are most often distributed via the Windows Store (but can also be side-loaded), and are most often packaged and distributed .

A computer with at least a 450MHz Pentium CPU with 128 MB of RAM, running Windows 2000, Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7, Windows 8/8.1, Windows 10, Windows Server 2012, Windows Server 2016 or Windows Server 2019 platforms. Instal

Windows 8.1 *6 Windows Server 2003 *7 Windows Server 2008 *8 Windows Server 2012 *9 Mac OS X *10: Supported *1 Printer drivers support both 32-bit and 64-bit Windows. *2 Microsoft Windows XP Professional Edition/Microsoft Windows XP Home Edition *3 Microsoft Windows Vista Ultimate/Microsoft Windows Vista Enterprise/Microsoft Windows Vista Business/

4/4 HMI software in the TIA Portal 4/5 SIMATIC WinCC (TIA Portal) Engineering 4/9 SIMATIC WinCC (TIA Portal) Runtime 4/10 WinCC Runtime Advanced 4/14 WinCC Runtime Professional 4/19 WinCC Runtime Communication 4/25 SIMATIC WinCC (TIA Portal) options 4/26 WinCC Recipes 4/28 WinCC Logging 4

Prosedur Akuntansi Hutang Jangka Pendek & Panjang BAGIAN PROYEK PENGEMBANGAN KUR IKULUM DIREKTORAT PENDIDIKAN MENENGAH KEJURUAN DIREKTORAT JENDERAL PENDIDIKAN DASAR DAN MENENGAH DEPARTEMEN PENDIDIKAN NASIONAL 2003 Kode Modul: AK.26.E.6,7 . BAGIAN PROYEK PENGEMBANGAN KURIKULUM DIREKTORAT PENDIDIKAN MENENGAH KEJURUAN DIREKTORAT JENDERAL PENDIDIKAN DASAR DAN MENENGAH DEPARTEMEN PENDIDIKAN .