Logout succeed
Logout succeed. See you again!

Windows 10 Development with XAML and C# 7 PDF
Preview Windows 10 Development with XAML and C# 7
Windows 10 Development with XAML and C# 7 Second Edition Jesse Liberty Jon Galloway Philip Japikse Jonathan Hartwell Windows 10 Development with XAML and C# 7 Jesse Liberty Philip Japikse Acton, Massachusetts, USA West Chester, Ohio, USA Jon Galloway Jonathan Hartwell Spring Valley, California, USA Plainfield, Illinois, USA ISBN-13 (pbk): 978-1-4842-2933-0 ISBN-13 (electronic): 978-1-4842-2934-7 https://doi.org/10.1007/978-1-4842-2934-7 Library of Congress Control Number: 2017962077 Copyright © 2018 by Jesse Liberty, Jon Galloway, Philip Japikse and Jonathan Hartwell Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/ 9781484229330. For more detailed information, please visit http://www.apress.com/source-code. Contents Chapter 1: Getting Started ��������������������������������������������������������������������1 Background �����������������������������������������������������������������������������������������������������������1 Versionless Windows 10 ����������������������������������������������������������������������������������2 The Microsoft Store �����������������������������������������������������������������������������������������2 Windows Design Guidelines ����������������������������������������������������������������������������������2 Being Fast and Fluid ����������������������������������������������������������������������������������������3 Sizing Beautifully ���������������������������������������������������������������������������������������������4 Using the Right Contracts ��������������������������������������������������������������������������������4 Investing in a Great Tile �����������������������������������������������������������������������������������4 Feeling like It Is Connected and Alive ��������������������������������������������������������������4 Roaming to the Cloud ��������������������������������������������������������������������������������������5 UX Guidelines ��������������������������������������������������������������������������������������������������5 Tooling �������������������������������������������������������������������������������������������������������������������5 Visual Studio 2017 �������������������������������������������������������������������������������������������5 Blend for Visual Studio 2017 �������������������������������������������������������������������������14 Git ������������������������������������������������������������������������������������������������������������������������18 Using Git in Visual Studio�������������������������������������������������������������������������������19 NuGet ������������������������������������������������������������������������������������������������������������������24 Installing Your First Package �������������������������������������������������������������������������25 Summary�������������������������������������������������������������������������������������������������������������27 Chapter 2: Building Your First Windows 10 UWP App ������������������������29 Creating Your First App ����������������������������������������������������������������������������������������29 App Project Overview ������������������������������������������������������������������������������������30 App�xaml ��������������������������������������������������������������������������������������������������������39 MainPage�xaml ����������������������������������������������������������������������������������������������39 Model View ViewModel (MVVM) ��������������������������������������������������������������������������40 The Pattern ����������������������������������������������������������������������������������������������������40 Creating a Model �������������������������������������������������������������������������������������������41 Creating the ViewModel ��������������������������������������������������������������������������������47 Updating the Code-Behind ����������������������������������������������������������������������������49 Creating the View ������������������������������������������������������������������������������������������49 Testing the App ����������������������������������������������������������������������������������������������51 Guidance ��������������������������������������������������������������������������������������������������������52 Navigation �����������������������������������������������������������������������������������������������������������52 Creating a New Page �������������������������������������������������������������������������������������53 Adding Navigation to the MainPage ��������������������������������������������������������������55 Handling the NavigatedTo Event ��������������������������������������������������������������������58 The Back Button ��������������������������������������������������������������������������������������������58 Summary�������������������������������������������������������������������������������������������������������������59 Chapter 3: Themes, Panels, and Controls �������������������������������������������61 Choosing a Theme ����������������������������������������������������������������������������������������������61 Using Panels �������������������������������������������������������������������������������������������������������64 The Canvas Control ����������������������������������������������������������������������������������������65 The Grid Control ���������������������������������������������������������������������������������������������69 The StackPanel Control ���������������������������������������������������������������������������������74 The RelativePanel Control �����������������������������������������������������������������������������76 The Border Control ����������������������������������������������������������������������������������������77 Working with Controls ����������������������������������������������������������������������������������������78 TextBlock and TextBox �����������������������������������������������������������������������������������79 Spell Check ����������������������������������������������������������������������������������������������������82 Headers and Watermarks Controls ����������������������������������������������������������������86 The PasswordBox Control ������������������������������������������������������������������������������89 Buttons and Event Handlers Controls ������������������������������������������������������������90 The CheckBoxes, ToggleSwitches, and RadioButtons Controls ���������������������95 The ListBox, ListView, and ComboBox Controls ���������������������������������������������98 The Image Control ���������������������������������������������������������������������������������������101 The Slider Control ����������������������������������������������������������������������������������������104 The ProgressBar Control ������������������������������������������������������������������������������104 The ToolTip Control ��������������������������������������������������������������������������������������106 The DatePickers and TimePickers Controls �������������������������������������������������106 Flyouts ���������������������������������������������������������������������������������������������������������108 Understanding Dependency Properties �������������������������������������������������������������111 Data Hiding ��������������������������������������������������������������������������������������������������111 Dependency Properties �������������������������������������������������������������������������������113 Summary�����������������������������������������������������������������������������������������������������������117 Chapter 4: Binding ����������������������������������������������������������������������������119 DataContext�������������������������������������������������������������������������������������������������������120 Creating a Simple Binding ��������������������������������������������������������������������������������120 Data-Binding Statements ����������������������������������������������������������������������������������123 Binding Errors ���������������������������������������������������������������������������������������������������125 FallbackValue ����������������������������������������������������������������������������������������������126 TargetNullValue ��������������������������������������������������������������������������������������������127 Binding to Elements ������������������������������������������������������������������������������������������127 Binding Modes ��������������������������������������������������������������������������������������������������130 UpdateSourceTrigger ����������������������������������������������������������������������������������������131 INotifyPropertyChanged ������������������������������������������������������������������������������������132 Binding to Collections ���������������������������������������������������������������������������������������136 Creating the Collection ��������������������������������������������������������������������������������136 Creating a Data-Bound ListView ������������������������������������������������������������������141 INotifyCollectionChanged ����������������������������������������������������������������������������143 Data Converters ������������������������������������������������������������������������������������������������144 Summary�����������������������������������������������������������������������������������������������������������149 Chapter 5: Local Data �����������������������������������������������������������������������151 Application Data ������������������������������������������������������������������������������������������������151 Settings Containers �������������������������������������������������������������������������������������152 Saving, Reading, and Deleting Local Data ��������������������������������������������������152 Creating the Data Layer ������������������������������������������������������������������������������������158 Creating the Repository Interface ����������������������������������������������������������������159 Creating the DataModel �������������������������������������������������������������������������������160 Creating the ViewModel ������������������������������������������������������������������������������161 Local Data ���������������������������������������������������������������������������������������������������������166 Using JSON to Format Data �������������������������������������������������������������������������166 Local Data Containers ���������������������������������������������������������������������������������167 Creating the File Repository ������������������������������������������������������������������������167 Creating the View ����������������������������������������������������������������������������������������173 Roaming ������������������������������������������������������������������������������������������������������179 User-Specified Locations ����������������������������������������������������������������������������������180 Creating the FileOperations Class ���������������������������������������������������������������180 Adding the File Association for JSON Files ��������������������������������������������������184 SQLite ����������������������������������������������������������������������������������������������������������������187 Summary�����������������������������������������������������������������������������������������������������������195 Chapter 6: Application Lifecycle �������������������������������������������������������197 The Three Application States ����������������������������������������������������������������������������198 Running �������������������������������������������������������������������������������������������������������199 Suspended ���������������������������������������������������������������������������������������������������199 Terminated���������������������������������������������������������������������������������������������������199 State Transitions �����������������������������������������������������������������������������������������������200 Launching ����������������������������������������������������������������������������������������������������201 Activating �����������������������������������������������������������������������������������������������������201 Suspending ��������������������������������������������������������������������������������������������������201 Resuming ����������������������������������������������������������������������������������������������������201 Terminating��������������������������������������������������������������������������������������������������201 Killing �����������������������������������������������������������������������������������������������������������202 Managing the Lifecycle �������������������������������������������������������������������������������������202 Building the Sample App �����������������������������������������������������������������������������������203 Adding the Navigation Parameter Class ������������������������������������������������������204 Creating the Details Page ����������������������������������������������������������������������������205 Creating the Main Page �������������������������������������������������������������������������������207 Handling Adding/Editing List Items ��������������������������������������������������������������209 Responding to App Suspension ������������������������������������������������������������������������210 The OnSuspending Event �����������������������������������������������������������������������������210 The EnteredBackground Event ��������������������������������������������������������������������211 Responding to App Activation ���������������������������������������������������������������������������216 Determining the Previous Application State ������������������������������������������������216 Testing the Restoring Navigation State �������������������������������������������������������216 Testing the Restoring Session Information ��������������������������������������������������217 Summary�����������������������������������������������������������������������������������������������������������218 Index �������������������������������������������������������������������������������������������������221 Getting Started Windows 10 Universal Windows Platform (UWP) development with C# and XAML carries a lot of similarities with developing Windows Presentation Foundation (WPF) applications. Well, they both use XAML and C#. Many of your existing skills with user interfaces (UIs) and program code can be leveraged for Windows 10 UWP apps. But there are a lot of differences, as well. The Universal Windows Platform is the major difference between UWP applications and WPF applications. An application written on the UWP can run on whatever hardware implements that functionality, which includes Xbox, Surface, PC, mobile, and even the Hololens. Apps are deployed through a central store (as opposed to click-once deployment or Microsoft Installer packages). Not a WPF developer? No worries! This book will take you through everything you need to know to build Windows 10 UWP apps. B ackground Microsoft released the latest revision of its Windows operating system, Windows 10, on July 29th, 2015. The release of the Universal Windows Platform is a continuation on the idea of writing code on one platform that could run on all supported devices. In order to get their users on the new operating system, Microsoft offered a free upgrade to Windows 10 to users who have a valid copy of Windows 7 or 8.1 and are not running an Enterprise edition. Chapter 1 GettinG Started Versionless Windows 10 With Microsoft’s policy change to auto update whenever there are updates available, Microsoft is moving away from Windows versions such as Windows 8.1 and moving toward build versions. Microsoft has announced that they will be doing feature updates semi-annually every March and September. The goal is to be able to have everybody on the same build, or as close to each other, as possible. If Microsoft is able to pull that off, it will be a major benefit for all Windows developers as it will allow us to better focus on supporting new features rather than worrying about supporting obsolete versions of Windows. The Microsoft Store How many times have you had to do tech support for a family member because he clicked on some random pop-up on the Internet or installed some software that a friend told him about? The main mechanism for getting apps is from the Microsoft Store. Having that one central place to get apps for Windows 10 helps prevent rogue software from getting installed, increasing the security and reliability of the device. It also provides a centralized location for developers to place their app for others to find. For more information about submitting your app to the Microsoft Store, see Chapter 12. Windows Design Guidelines In order to get your apps accepted into the store, you must make sure they meet the seven traits of a great app and follow the five Microsoft design principles. There are additional technical requirements that will be discussed in Chapter 11. 2 Chapter 1 GettinG Started Let’s look at the seven traits of a great app first. To achieve greatness, it must: • Be fast and fluid • Size beautifully • Use the right contracts • Invest in a great tile • Feel like it is connected and alive • Roam to the cloud • Embrace modern app design principles Being Fast and Fluid Modern apps can run on a variety of devices with a wide range of capabilities. While Microsoft has set minimum standards for all hardware that carries the Windows 10 logo, it’s important for the success of your app (as well as the success of Windows 10) that your app doesn’t perform poorly or cause the hardware to perform poorly. You will see as you work your way through this book that in order to develop Windows 10 UWP applications, you must use asynchronous programming to ensure a responsive UI. Additionally, the very design of the Windows 10 UWP process lifetime management cycle ensures that background apps don’t drain the battery or use up precious system resources. Use the async pattern liberally. If your app is taking a long time to load or to run, people will uninstall it. Or, worse yet, they will write a scathing review and then uninstall it. 3