Page 2
Table of Contents Mastering Windows Presentation Foundation Credits About the Author About the Reviewer www.PacktPub.com Why subscribe? Customer Feedback Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions 1. A Smarter Way of Working with WPF What is MVVM and how does it help? Models View Models Views Data binding So how does MVVM help? Is there a downside? Debunking the myth about code behind Learning how to communicate again Introducing the ICommand interface Handling events in Attached Properties Making use of delegates Structuring the application code base Summary 2. Debugging WPF Applications Utilizing the Output window Putting Presentation Trace Sources to work Discovering inner exceptions Debugging data bound values
Page 3
Outputting values to UI controls Catching changing Dependency Property values Exploiting converters Summary 3. Writing Custom Application Frameworks What is an application framework? Encapsulating common functionality In base classes Through interfaces With Extension methods In UI controls With converters Constructing a custom application framework Separating the Data Access Layer Providing services Implementing Dependency Injection Connecting Views with View Models Summary 4. Becoming Proficient with Data Binding Data binding basics Binding path syntax Escaping invalid characters Exploring the Binding class Directing data bound traffic Binding to different sources Binding with priority Binding from within control templates Binding source changes Converting data bound values Binding multiple sources to a single target property Dependency Properties Setting metadata Declaring read-only Dependency Properties Registering Attached Properties Prioritizing value setting sources Data templates Taking complete control Displaying hierarchical data Data binding to enumeration collections Summary 5. Using the Right Controls for the Job Investigating the built-in controls Inheriting framework abilities
Page 4
Laying it on the line Containing controls Canvas DockPanel Grid StackPanel UniformGrid WrapPanel Providing custom layout behavior Content controls Presenting content Items controls Adorners Modifying existing controls Styling Being resourceful Merging resources Triggering changes Templating controls Attaching properties Combining controls Creating custom controls Summary 6. Mastering Practical Animations Investigating timelines Introducing key-frames Telling stories Controlling storyboards Easing functions Animating along a path Creating everyday animations Summary 7. Creating Visually Appealing User Interfaces Styling applications consistently Overriding default control styles Using professional icons Layering visuals Throwing shadows Declaring multiple borders Reusing composite visuals Reflecting light Creating glowing effects Putting it all together
Page 5
Moving away from the ordinary Casting reflections Exploring borderless windows Visualizing data Livening up UI controls Summary 8. Implementing Responsive Data Validation Using validation rules - To do or not to do? Getting to grips with validation interfaces Implementing the IDataErrorInfo interface Introducing the INotifyDataErrorInfo interface Annotating data Varying levels of validation Incorporating multiple validation techniques Customizing the error template Avoiding UI-based validation errors Amalgamating validation and visuals Summary 9. Completing That Great User Experience Providing user feedback Utilizing multiple threads Discovering the Async and Await keywords Building asynchrony into our framework Going the extra mile Producing in-application help Enabling user preferences Extending common courtesies Un-burdening the end user Summary 10. Improving Application Performance Leveraging the power of hardware rendering Making more efficient resources Freezing objects Using the right controls for performance Drawing conclusions Imaging more efficiently Enhancing the performance of textual output Liking the linking Data binding Registering Dependency Properties Binding to collections Shrinking data objects Virtualizing collections
Page 6
Handling events Summary 11. Deploying Your Masterpiece Application Installing Windows applications Utilizing ClickOnce functionality Securing deployments Isolating storage Accessing application versions Summary 12. What Next? Turning attention to future projects Improving our application framework Logging errors Using online resources
Page 8
Mastering Windows Presentation Foundation Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: February 2017 Production reference: 1130217 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-300-2 www.packtpub.com
Page 9
Credits Author Copy Editor Sheridan Yuen Pranjali Chury Reviewer Project Coordinator Alex Golesh Vaidehi Sawant Commissioning Editor Proofreader Edward Gordon Safis Editing Acquisition Editor Indexer Chaitanya Nair Rekha Nair Content Development Editor Graphics Zeeyan Pinheiro Kirk D'Penha Technical Editor Production Coordinator Kunal Chaudhari Shantanu N. Zagade
Page 10
About the Author Sheridan Yuen is a Microsoft .NET MCTS and Oracle Java SCJP certified software developer, living in London, England. His passion for coding made him stand out from the crowd right from the start. From his second year onward at university, he was employed to be a teaching assistant for the first year student coding workshops and has since returned as a guest lecturer. Among other prestigious positions, he was the primary software developer for the Ministry of Sound group for four years, working on their main music business application, responsible for creating their multi award winning albums. This application managed to increase its users’ productivity by up to 80% in some cases. In addition to this, he architected a unique ticket scanning application for their award winning nightclub, making it the first club in the world to introduce scanned ticket entry across all streams for their clients. Coming from a musical background and being a qualified audio engineer, with experience of record production and digital audio, this post was a perfect union. He soon became a popular figure in the C# and WPF sections of the Stack Overflow, “question and answer” website, being awarded enough reputation by the community members to raise him to well within the top half percent of all users. While authoring this book and other projects have kept him away for some time, he is keen to return to continue to help new users to get to grips with WPF. I would like to thank my long suffering girlfriend Jemma, who has regularly had to make do without my company for the best part of a year, for her patience while I was composing and writing this book and the many examples in it. I’d also like to thank Chaitanya from Packt Publishing for convincing me to write this book in the first place and without who, this book would not have been written. Finally, I would like to thank Mary Thomson, Professor Sarah Barman and Professor James Orwell in particular, from Kingston University, London, who inspired me to change the direction of my previous career and planted the seed of curiosity that has taken me so far. I would also like to thank James for encouraging me to move from the Bachelor’s Degree course to the integrated Master’s Degree that I ended up gaining and for all of the benefits that this brought with it.
Page 11
About the Reviewer Alex Golesh is an international expert in XAML-based technologies such as Universal Windows Platform (Windows, Windows Phone, HoloLens, Xbox One), Xamarin.Forms, WPF, and Silverlight. Also, Alex specializes in cloud-based solutions such as Microsoft Azure. Alex developed training solutions for Microsoft Learning on Windows Phone and Windows 8 and delivers workshops for developers and enterprises. Alex leads the architecture and development process in multiple projects for his clients.
Page 12
www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Page 13
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://goo.gl/yuUApD. If you'd like to join our team of regular reviewers, you can email us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Page 14
Preface While it can be easy to construct a basic form using WPF, it takes a lot more to fully understand what WPF can do for us and how best to use it. It has a steep learning curve and it can be difficult to comprehend this very different way of working. This book aims to help you to get over that initial hill and continue to fully enable you to implement any given requirement. This book will start by providing you the foundation knowledge on how to improve your workflow and what to do when you have problems. It will build upon this foundation by introducing the base layer of the application that will serve all that comes after it. We will then take a detour to cover data binding in detail. The book will then turn to the User Interface (UI) and how to get the most out of the built-in and custom WPF controls. It will make clear which customization methods are best to utilize in a wide range of scenarios, avoiding the need to rewrite existing functionality. Other tips and tricks will also be provided to enable you to create your own visually stunning UIs. The final section of the book will introduce the concluding ways for you to polish your applications, from adding practical animations and data validation, to improving application performance. The book will end by explaining how to deploy the applications that you have been working so hard on and discuss other things that you can now achieve with your new found knowledge.
Page 15
What this book covers Chapter 1, A Smarter Way of Working with WPF, introduces the Model, View, View Model (MVVM) software architectural pattern and the benefits of using it with WPF. Chapter 2, Debugging WPF Applications, provides essential tips on various methods of debugging WPF applications, ensuring the ability to iron out any problems that may occur. Chapter 3, Writing Custom Application Frameworks, introduces the indispensable concept of application frameworks, with early examples that will be built upon as the book progresses. By the end of the book, you will have a fully functioning Framework with which to build your applications upon. Chapter 4, Becoming Proficient with Data Binding, demystifies data binding and clearly demonstrates how to use it in a practical application. A plethora of examples will help you to understand which binding syntax to use in any given situation and to be confident that their bindings will work as expected. Chapter 5, Using The Right Controls for The Job, explains which controls to use in particular situations and describes the various ways to modify them when required. It clearly outlines how to customize existing controls and how to create custom controls when required. Chapter 6, Mastering Practical Animations, explains the ins and outs of WPF Animations, detailing lesser known functionality. It concludes with a number of ideas for practical animations and continues to build upon the custom application framework. Chapter 7, Creating Visually Stunning User Interfaces, offers advice for getting the most out of the WPF visuals, while remaining practical, and provides handy tips on making applications stand out from the crowd. Chapter 8, Implementing Responsive Data Validation, presents a number of methods of data validation to suit every situation and continues to build upon the custom application framework. It covers full, partial, instant, and delayed validation and a variety of different ways to display validation errors. Chapter 9, Completing That Great User Experience, provides tips for creating applications with a great user experience. Concepts introduced here, such as asynchronous data access and keeping the end users well informed, will substantially improve the existing custom application framework. Chapter 10, Improving Application Performance, lists a number of ways to increase the performance of WPF applications from freezing resources to implementing virtualization. Readers that follow these tips and tricks can rest assured that their WPF applications will perform as optimally as they can. Chapter 11, Deploying Your Masterpiece Application, covers the final requirement for all professional applications—deployment. It includes the older method of using the Windows Installer software, along with the more common and up-to-date method of using ClickOnce functionality. Chapter 12, What Next?, summarizes what you have learned from this book and suggests what you can do with many of your various new skills. It provides you with further ideas on extending the application framework.
Page 17
What you need for this book As with all WPF development, you'll need to have the .NET Framework and a version of Microsoft's Visual Studio integrated development environment software installed on your computer. You'll be able to use versions as old as 2010, but in order to use the code in the book that takes advantage of the latest .NET Framework improvements, you'll need to use one of the newer versions. Note that any edition of Visual Studio can be used, from the top of the line Enterprise edition to the free Community (2015) edition.
Page 18
Who this book is for This book is for working developers with a basic to moderate level of knowledge about Windows Presentation Foundation and for those interested in improving their practical day to day WPF skills. It will also be of special interest to individuals wanting to know more about application architecture and those wanting to improve the look of their user interfaces.
Page 19
Conventions In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "There are two other useful properties declared by the Grid class." A block of code is set as follows: public string Name { get { return name; } set { if (name != value) { name = value; NotifyPropertyChanged("Name"); } } } Any command-line input or output is written as follows: System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(Validation.Errors)[0].ErrorContent; DataItem='TextBox' (Name=''); target element is 'TextBox' (Name=''); target property is 'ToolTip' (type 'Object') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The Cancel button has been declared in the second row and column." Note Warnings or important notes appear in a box like this. Tip Tips and tricks appear like this.
Page 20
Reader feedback Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e- mail feedback@packtpub.com, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Showing the first 20 pages. Download the document to see it all.
Comments (0)
No comments yet. Be the first!