You are previewing Universal Windows® Apps with XAML and C# Unleashed.
O'Reilly logo
Universal Windows® Apps with XAML and C# Unleashed

Book Description

Full Color: Figures and code appear as they do in Visual Studio.

Universal Windows apps are a total game-changer. You can now write once and deploy your app to any modern PC, tablet, phone, or other Windows device. Suddenly, it’s quick and easy to serve new devices and form factors, sharing unified assets ranging from code to styles to user controls. It has never been easier to reach multiple-device markets!

Now, the world’s #1 expert on Microsoft XAML brings together all the knowledge Windows developers need to build universal apps that adapt smoothly for any device running Windows 8.1, Windows Phone 8.1, or their successors. Drawing on his unsurpassed personal experience and direct input from Microsoft’s Windows and Visual Studio teams, Adam Nathan illuminates core development concepts, answers your most important questions, and candidly assesses the technology’s strengths and limitations.

This full-color guide walks you through every key task involved in building a universal Windows app: layout and input, working with the app model, using advanced controls, leveraging XAML’s powerful rich media features, and much more.

Detailed information on how to...

  • Quickly build universal, localized apps that exploit the vast global scale of the Windows Store

  • Size, position, and transform elements within layouts that gracefully adapt to different devices

  • Handle input from touch, mouse, pen, keyboard, and any sensor

  • Monetize apps with Windows Store sales, in-app purchases, and advertising

  • Make the most of controls for managing content, items, text, images, media, speech, and more

  • Create efficient 2D vector graphics and animations

  • Use styles, templates, and visual states to redesign controls without losing functionality

  • Bind data sources to simplify data presentation and updates

  • Easily integrate data from apps, users, and networks

  • Support Windows app commands: Search, Share, Print, Play, Project, Settings

  • Use contracts to build apps that cooperate to perform complex tasks

  • Improve the user experience even when your app isn’t running

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Contents at a Glance
    5. Table of Contents
    6. About the Author
    7. Dedication
    8. Acknowledgments
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
      1. Who Should Read This Book?
      2. Software Requirements
      3. Code Examples
      4. How This Book Is Organized
        1. Part I: Getting Started
        2. Part II: Building an App
        3. Part III: Working with the App Model
        4. Part IV: Understanding Controls
        5. Part V: Leveraging the Richness of XAML
        6. Part VI: Exploiting Windows
      5. Conventions Used in This Book
    12. Part I: Getting Started
      1. Chapter 1. Hello, Real World!
        1. Creating, Deploying, and Profiling an App
        2. Understanding the App Packages
          1. Application
          2. Visual Assets
          3. Requirements
          4. Capabilities
          5. Declarations
          6. Content URIs
          7. Packaging
        3. Updating XAML and C# Code
          1. The Main Page User Interface
          2. The Main Page Logic
          3. The Application Definition
        4. Making the App World-Ready
        5. Making the App Accessible
          1. Improving Screen Reading
          2. Handling High Contrast Themes
        6. Submitting to the Windows Store
        7. Summary
      2. Chapter 2. Mastering XAML
        1. Elements and Attributes
        2. Namespaces
        3. Property Elements
        4. Type Converters
        5. Markup Extensions
        6. Children of Object Elements
          1. The Content Property
          2. Collection Items
          3. More Type Conversion
        7. Mixing XAML with C#
          1. Loading and Parsing XAML at Runtime
          2. Naming XAML Elements
          3. Visual Studio’s Support for XAML and Code-Behind
        8. XAML Keywords
        9. Summary
    13. Part II: Building an App
      1. Chapter 3. Sizing, Positioning, and Transforming Elements
        1. Controlling Size
          1. Height and Width
          2. Margin and Padding
        2. Controlling Position
          1. Alignment
          2. Content Alignment
          3. FlowDirection
        3. Applying 2D Transforms
          1. RotateTransform
          2. ScaleTransform
          3. SkewTransform
          4. TranslateTransform
          5. Combining Transforms
        4. Applying 3D Transforms
        5. Summary
      2. Chapter 4. Layout
        1. Discovering Your Window Size and Location
          1. Choosing a Minimum Width
          2. Discovering the Current Orientation
        2. Panels
          1. Canvas
          2. StackPanel
          3. Grid
          4. VariableSizedWrapGrid
        3. Handling Content Overflow
          1. Clipping
          2. Scrolling
          3. Scaling
        4. Summary
      3. Chapter 5. Handling Input: Touch, Mouse, Pen, and Keyboard
        1. Touch Input
          1. Pointers
          2. Gestures
          3. Manipulations
        2. Mouse Input
          1. MouseDevice and MouseCapabilities
          2. Revisiting Pointer Events and PointerPointProperties
          3. Revisiting GestureRecognizer
          4. Revisiting UIElement Gesture Events
        3. Pen Input
          1. Rendering Handwriting
          2. Handwriting Recognition
        4. Keyboard Input
          1. UIElement Keyboard Events
          2. Getting Key States at Any Time
          3. Keyboard Modifiers in Pointer Events
          4. Focus
        5. Summary
    14. Part III: Working with the App Model
      1. Chapter 6. App Lifecycle
        1. Killing
        2. Suspending
          1. Handling the Suspending Event
          2. Deferral
        3. Resuming
        4. Terminating
        5. Launching
          1. LaunchActivatedEventArgs
          2. Properly Reacting to PreviousExecutionState
        6. Activating
        7. Managing Session State with SuspensionManager
          1. A Scheme for Representing Session State
          2. Introducing SuspensionManager
        8. Programmatically Launching Apps
          1. Launching an App for a File
          2. Launching an App for a URI
          3. Customizing App Launch
        9. Summary
      2. Chapter 7. Threading, Windows, and Pages
        1. Understanding the Threading Model for Universal Apps
          1. Awaiting an Asynchronous Operation
          2. Transitioning Between Threads
        2. Displaying Multiple Windows
        3. Navigating Between Pages
          1. Basic Navigation and Passing Data
          2. Navigating Forward and Back
          3. Page Caching
          4. NavigationHelper
          5. Other Ways to Use Frame
        4. Summary
      3. Chapter 8. The Many Ways to Earn Money
        1. Adding Advertisements to Your App
          1. Defining Ad Units in pubCenter
          2. Adding PC Ads to a Page
          3. Adding Phone Ads to a Page
          4. Advanced AdControl Behaviors
        2. Supporting a Free Trial
          1. Time-Based Trials
          2. Feature-Differentiated Trials
          3. Helping the User Purchase a Full License
        3. Supporting In-App Purchases
          1. Durable Products
          2. Consumable Products
        4. Validating Windows Store Receipts
        5. Testing Windows Store Features
          1. Working with CurrentAppSimulator
          2. Automated Testing
        6. Summary
    15. Part IV: Understanding Controls
      1. Chapter 9. Content Controls
        1. Button
          1. Associating a Flyout
          2. ButtonBase Behaviors
        2. AppBarButton
          1. SymbolIcon
          2. FontIcon
          3. PathIcon
          4. BitmapIcon
        3. HyperlinkButton
        4. RepeatButton
        5. ToggleButton
        6. AppBarToggleButton
        7. CheckBox
        8. RadioButton
        9. ToolTip
        10. App Bars
          1. AppBar
          2. CommandBar
        11. Summary
      2. Chapter 10. Items Controls
        1. Items in the Control
        2. Items Panels
        3. ComboBox
        4. ListBox
        5. ListView
          1. Richer ScrollIntoView
          2. Header and Footer
          3. Selection
          4. Reordering Items
          5. Data Virtualization
          6. Incremental Item Rendering
        6. GridView
        7. FlipView
        8. SemanticZoom
        9. MenuFlyout
        10. Summary
      3. Chapter 11. Text
        1. TextBlock
          1. Customizing Text Rendering
          2. Text Content
          3. Text Elements
          4. Text Selection
        2. RichTextBlock
          1. Embedding UIElements
          2. Text Overflow
        3. TextBox
          1. Spelling and Text Prediction
          2. Text Selection
          3. InputScope and the Software Keyboard
        4. RichEditBox
        5. PasswordBox
        6. Summary
      4. Chapter 12. Images
        1. The Image Element
          1. Referencing Files with URIs
          2. Custom Stretching with Nine-Grid
          3. Generating Dynamic Images with WriteableBitmap
          4. Generating Dynamic Images with RenderTargetBitmap
        2. Multiple Files for Multiple Environments
          1. Loading File Variations Automatically
          2. Loading File Variations Manually
          3. Leveraging Resource Packages
        3. Decoding Images
          1. Getting Pixel Data
          2. Reading Metadata
        4. Encoding Images
          1. Writing Pixel Data
          2. Writing Metadata
          3. Transcoding
        5. Rendering PDF Content as an Image
          1. Rendering a PDF Page
          2. Customizing the Page Rendering
        6. Summary
      5. Chapter 13. Audio, Video, and Speech
        1. Playback
          1. The Media Content
          2. Customizing Playback
          3. States and Events
          4. Markers
          5. Adding Effects
          6. Using MediaElement as a Media Player
          7. MediaPlayer
          8. Playing Custom Media Formats
        2. Capture
          1. CameraCaptureUI
          2. CaptureElement
        3. Transcoding
          1. Changing the Quality
          2. Changing the Format
          3. Trimming
          4. Adding Effects
        4. Speech Synthesis
          1. Bringing Text to Life
          2. Speech Synthesis Markup Language
        5. Summary
      6. Chapter 14. Other Controls
        1. Range Controls
          1. ProgressBar
          2. Slider
        2. SearchBox
          1. Providing Suggestions
        3. Popup Controls
          1. MessageDialog
          2. PopupMenu
          3. Popup
        4. Hub
          1. HubSections
          2. Jumping to a HubSection
        5. Date and Time Controls
          1. DatePicker
          2. TimePicker
        6. ProgressRing
        7. ToggleSwitch
        8. WebView
          1. Navigation
          2. Composing HTML with XAML
          3. Composing JavaScript with C#
          4. Capturing Web Content
        9. Summary
    16. Part V: Leveraging the Richness of XAML
      1. Chapter 15. Vector Graphics
        1. Shapes
          1. Rectangle
          2. Ellipse
          3. Line
          4. Polyline
          5. Polygon
          6. Path
          7. Getting Fancy with Strokes
        2. Geometries
          1. PathFigures and PathSegments
          2. GeometryGroup
          3. Representing Geometries as Strings
        3. Brushes
          1. Color Brushes
          2. Tile Brushes
        4. Summary
      2. Chapter 16. Animation
        1. Dependency Properties
        2. Theme Transitions
          1. Applying Theme Transitions to Elements
          2. The Eight Theme Transitions
        3. Theme Animations
          1. Using a Storyboard
          2. The 14 Theme Animations
          3. Tweaking Theme Animations
        4. Custom Animations
          1. Independent versus Dependent Animations
          2. Controlling Duration
          3. Flexibility with From and To
          4. Tweaking Animations with Timeline Properties
          5. Storyboards with Multiple Animations
          6. Property Paths
          7. Tweaking Storyboards with Timeline Properties
        5. Custom Keyframe Animations
          1. Linear Keyframes
          2. Spline Keyframes
          3. Discrete Keyframes
          4. Easing Keyframes
        6. Easing Functions
          1. Power Easing Functions
          2. Other Easing Functions
        7. Manual Animations
        8. Summary
      3. Chapter 17. Styles, Templates, and Visual States
        1. Styles
          1. Using a Base TargetType
          2. Style Inheritance
          3. Implicit Styles
          4. Theme Resources
          5. More About Resource Lookup
        2. Templates
          1. Introducing Control Templates
          2. Respecting the Target Control’s Properties
          3. Setting Template Inside a Style
        3. Visual States
          1. Responding to Visual State Changes
          2. Visual Transitions
        4. Summary
      4. Chapter 18. Data Binding
        1. Introducing Binding
          1. Using Binding in C#
          2. Binding to Plain Properties
          3. Customizing the Data Flow
          4. Sharing the Source with DataContext
          5. Binding to a Collection
        2. Controlling Rendering
          1. Using Data Templates
          2. Using Value Converters
        3. Customizing the View of a Collection
          1. Grouping
          2. Navigating
        4. High-Performance Rendering with ListView and GridView
        5. Summary
    17. Part VI: Exploiting Windows
      1. Chapter 19. Working with Data
        1. An Overview of Files and Folders
        2. App Data
          1. App Settings
          2. App Files
        3. User Data
          1. File Picker
          2. Folder Picker
          3. Libraries and Other Special Folders
        4. Networking
          1. HTTP Requests
          2. Background Transfer
          3. Sockets
          4. Syndication
          5. Connection Info
        5. Summary
      2. Chapter 20. Supporting App Commands
        1. Search
          1. The Search Contract
          2. Customizing the Search Pane
          3. Programmatically Showing the Search Pane
        2. Share
          1. Being a Share Source
          2. Being a Share Target
        3. Print
          1. Changing Default Options
          2. Configuring Displayed Options
          3. Adding Custom Options
        4. Play
        5. Project
        6. Settings
          1. Showing Links
          2. Handling Links with SettingsPane
        7. Summary
      3. Chapter 21. Leveraging Contracts
        1. Account Picture Provider
        2. AutoPlay Content and AutoPlay Device
        3. File Type Associations
        4. Protocol
        5. File Open Picker
        6. File Save Picker
        7. Contact Picker
          1. Being a Contact Picker Source
          2. Being a Contact Picker Target
        8. The Contact Contract
          1. Being a Contact Source
          2. Being a Contact Target
        9. The Appointments Provider Contract
          1. Being an Appointments Provider Source
          2. Being an Appointments Provider Target
        10. Background Tasks
          1. Background Audio
          2. Custom Background Tasks
        11. Summary
      4. Chapter 22. Reading from Sensors
        1. Accelerometer
          1. Tossing Motion
          2. Shake Detection
        2. Gyrometer
        3. Inclinometer
        4. Compass
        5. Light Sensor
        6. Orientation
          1. SimpleOrientationSensor
          2. OrientationSensor
        7. Location
          1. Getting the Current Location
          2. Understanding the Current Location
          3. Implementing Geofencing
        8. Proximity
          1. Sending and Receiving Messages
          2. Finding and Communicating with a Peer Device
        9. Summary
      5. Chapter 23. Controlling Devices
        1. Fingerprint Readers
        2. Image Scanners
          1. Getting an Instance of ImageScanner
          2. Performing a Scan
          3. Configuring Scan Sources
          4. Showing a Preview
        3. Barcode Scanners
          1. Getting an Instance of BarcodeScanner
          2. Claiming the Device
          3. Enabling the Device
          4. Retrieving the Data
        4. Magnetic Stripe Readers
          1. Three Types of Magnetic Stripe Cards
          2. Retrieving the Data
        5. Custom Bluetooth Devices
          1. Declaring the Right Device Capability
          2. Connecting to the Device
          3. Communicating with the Device
        6. Custom Bluetooth Smart Devices
          1. Declaring the Right Device Capability
          2. Connecting to the Device
          3. Communicating with the Device
        7. Custom USB Devices
          1. Declaring the Right Device Capability
          2. Connecting to the Device
          3. Communicating with the Device
        8. Custom HID Devices
          1. Declaring the Right Device Capability
          2. Connecting to the Device
          3. Communicating with the Device
        9. Custom Wi-Fi Direct Devices
        10. Summary
      6. Chapter 24. Thinking Outside the App: Live Tiles, Notifications, and the Lock Screen
        1. Live Tiles
          1. Tile Templates
          2. Options for Updating Tiles
        2. Badges
        3. Secondary Tiles
        4. Toast Notifications
          1. Toast Templates
          2. Responding to a Clicked Toast Notification
          3. Alarm and Incoming Call Notifications
          4. Options for Showing Toast Notifications
        5. Setting Up Push Notifications
          1. Using the Push Notification Wizard
          2. Server-Side Code
          3. Client-Side Code
        6. The Lock Screen
        7. Summary
    18. Index
    19. Code Snippets