ebook img

Pro WPF 4.5 in VB: Windows Presentation Foundation in .NET 4.5 PDF

1078 Pages·2012·22.036 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro WPF 4.5 in VB: Windows Presentation Foundation in .NET 4.5

THE EXPERT’S VOICE® IN .NET www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info Contents at a Glance About the Author �������������������������������������������������������������������������������������������������������������xxxi About the Technical Reviewer ���������������������������������������������������������������������������������������xxxiii Acknowledgments ����������������������������������������������������������������������������������������������������������xxxv Introduction ������������������������������������������������������������������������������������������������������������������xxxvii ■ Part 1: Fundamentals �����������������������������������������������������������������������������������1 ■ Chapter 1: Introducing WPF �����������������������������������������������������������������������������������������������3 ■ Chapter 2: XAML ��������������������������������������������������������������������������������������������������������������19 ■ Chapter 3: Layout ������������������������������������������������������������������������������������������������������������51 ■ Chapter 4: Dependency Properties ����������������������������������������������������������������������������������91 ■ Chapter 5: Routed Events ����������������������������������������������������������������������������������������������103 ■ Part 2: Deeper into WPF ����������������������������������������������������������������������������141 ■ Chapter 6: Controls ��������������������������������������������������������������������������������������������������������143 ■ Chapter 7: The Application ��������������������������������������������������������������������������������������������195 ■ Chapter 8: Element Binding �������������������������������������������������������������������������������������������227 ■ Chapter 9: Commands ���������������������������������������������������������������������������������������������������243 ■ Chapter 10: Resources ��������������������������������������������������������������������������������������������������269 ■ Chapter 11: Styles and Behaviors ���������������������������������������������������������������������������������283 ■ Part 3: Drawing �����������������������������������������������������������������������������������������305 ■ Chapter 12: Shapes, Brushes, and Transforms ��������������������������������������������������������������307 ■ Chapter 13: Geometries and Drawings ��������������������������������������������������������������������������345 v www.it-ebooks.info ■ Contents at a GlanCe ■ Chapter 14: Effects and Visuals �������������������������������������������������������������������������������������367 ■ Chapter 15: Animation Basics ���������������������������������������������������������������������������������������389 ■ Chapter 16: Advanced Animation ����������������������������������������������������������������������������������429 ■ Part 4: Templates and Custom Elements ��������������������������������������������������461 ■ Chapter 17: Control Templates ��������������������������������������������������������������������������������������463 ■ Chapter 18: Custom Elements ���������������������������������������������������������������������������������������503 ■ Part 5: Data �����������������������������������������������������������������������������������������������553 Chapter 19: Data Binding�����������������������������������������������������������������������������������������������555 Chapter 20: Formatting Bound Data ������������������������������������������������������������������������������599 Chapter 21: Data Views �������������������������������������������������������������������������������������������������645 Chapter 22: Lists, Trees, and Grids ��������������������������������������������������������������������������������663 Part 6: Windows, Pages, and Rich Controls ����������������������������������������������701 Chapter 23: Windows ����������������������������������������������������������������������������������������������������703 Chapter 24: Pages and Navigation ��������������������������������������������������������������������������������733 ■ Chapter 25: Menus, Toolbars, and Ribbons �������������������������������������������������������������������777 ■ Chapter 26: Sound and Video ����������������������������������������������������������������������������������������801 ■ Chapter 27: 3-D Drawing �����������������������������������������������������������������������������������������������823 ■ Part 7: Documents and Printing ���������������������������������������������������������������867 ■ Chapter 28: Documents �������������������������������������������������������������������������������������������������869 ■ Chapter 29: Printing ������������������������������������������������������������������������������������������������������921 ■ Part 8: Additional Topics���������������������������������������������������������������������������951 ■ Chapter 30: Interacting with Windows Forms ���������������������������������������������������������������953 ■ Chapter 31: Multithreading �������������������������������������������������������������������������������������������969 ■ Chapter 32: The Add-in Model ���������������������������������������������������������������������������������������983 ■ Chapter 33: ClickOnce Deployment �����������������������������������������������������������������������������1007 Index �������������������������������������������������������������������������������������������������������������������������������1027 vi www.it-ebooks.info Introduction When .NET first appeared, it introduced a small avalanche of new technologies. There was a whole new way to write web applications (ASP.NET), a whole new way to connect to databases (ADO.NET), new typesafe languages (C# and VB .NET), and a managed runtime (the CLR). Not least among these new technologies was Windows Forms, a library of classes for building Windows applications. Although Windows Forms is a full-featured toolkit, it’s hardwired to old, essential bits of Windows plumbing. Most significantly, Windows Forms relies on the Windows API to create the visual appearance of standard user interface elements such as buttons, text boxes, check boxes, and so on. As a result, these ingredients are essentially uncustomizable. For example, if you want to create a stylish glow button you need to create a custom control and paint every aspect of the button (in all its different states) using a lower-level drawing model. Even worse, ordinary windows are carved up into distinct regions, with each control getting its own piece of real estate. As a result, there’s no good way for the painting in one control (for example, the glow effect behind a button) to spread into the area owned by another control. And don’t even think about introducing animated effects such as spinning text, shimmering buttons, shrinking windows, or live previews because you’ll have to paint every detail by hand. The Windows Presentation Foundation (WPF) changed all this by introducing a model with entirely different plumbing. Although WPF includes the standard controls you’re familiar with, it draws every text, border, and background fill itself. As a result, WPF can provide much more powerful features that let you alter the way any piece of screen content is rendered. Using these features, you can restyle common controls such as buttons, often without writing any code. Similarly, you can use transformation objects to rotate, stretch, scale, and skew anything in your user interface, and you can even use WPF’s baked-in animation system to do it right before the user’s eyes. And because the WPF engine renders the content for a window as part of a single operation, it can handle unlimited layers of overlapping controls, even if these controls are irregularly shaped and partially transparent. Underlying WPF is a powerful infrastructure based on DirectX, the hardware-accelerated graphics API that’s commonly used in cutting-edge computer games. This means that you can use rich graphical effects without incurring the performance overhead that you’d suffer with Windows Forms. In fact, you even get advanced features such as support for video files and 3-D content. Using these features (and a good design tool), it’s possible to create eye-popping user interfaces and visual effects that would have been all but impossible with Windows Forms. It’s also important to note that you can use WPF to build an ordinary Windows application with standard controls and a straightforward visual appearance. In fact, it’s just as easy to use common controls in WPF as it is in the older Windows Forms model. Even better, WPF enhances features that appeal directly to business developers, including a vastly improved data binding model, a set of classes for printing content and managing print queues, and a document feature for displaying large amounts of formatted text. You’ll even get a model for building page-based applications that run seamlessly in Internet Explorer and can be launched from a website, all without the usual security warnings and irritating installation prompts. Overall, WPF combines the best of the old world of Windows development with new innovations for building modern, graphically rich user interfaces. xxxvii www.it-ebooks.info ■ IntroduCtIon About This Book This book is an in-depth exploration of WPF for professional developers who know the .NET platform, the VB language, and the Visual Studio development environment. Experience with previous versions of WPF is not required, although new features are highlighted with a “What’s New” box at the beginning of each chapter for more seasoned WPF developers. This book provides a complete description of every major WPF feature, from XAML (the markup language used to define WPF user interfaces) to 3-D drawing and animation. Along the way, you’ll occasionally work with code that involves other features of the .NET Framework, such as the ADO.NET classes you use to query a database. These features aren’t discussed here. Instead, if you want more information about .NET features that aren’t specific to WPF, you can refer to one of the many dedicated .NET titles from Apress. The following list gives you a quick preview of each chapter: Chapter 1: Introducing WPF describes the architecture of WPF, its DirectX plumbing, and the new device-independent measurement system that resizes user interfaces automatically. Chapter 2: XAML describes the XAML standard that you use to define user interfaces. You’ll learn why it was created and how it works, and you’ll create a basic WPF window using different coding approaches. Chapter 3: Layout delves into the layout panels that allow you to organize elements in a WPF window. You’ll consider different layout strategies, and you’ll build some common types of windows. Chapter 4: Dependency Properties describes how WPF uses dependency properties to provide support for key features such as data binding and animation. Chapter 5: Routed Events describes how WPF uses event routing to send events bubbling or tunneling through the elements in your user interface. It also describes the basic set of mouse, keyboard, and multitouch events that all WPF elements support. Chapter 6: Controls considers the controls every Windows developer is familiar with, such as buttons, text boxes, and labels—and their WPF twists. Chapter 7: The Application introduces the WPF application model. You’ll see how to create single-instance and document-based WPF applications. Chapter 8: Element Binding introduces WPF data binding. You’ll see how to bind any type of object to your user interface. Chapter 9: Commands introduces the WPF command model, which allows you to wire multiple controls to the same logical action. Chapter 10: Resources describes how resources let you embed binary files in your assembly and reuse important objects throughout your user interface. Chapter 11: Styles and Behaviors explains the WPF style system, which lets you apply a set of common property values to an entire group of controls. xxxviii www.it-ebooks.info ■ IntroduCtIon Chapter 12: Shapes, Brushes, and Transforms introduces the 2-D drawing model in WPF. You’ll learn to create shapes, alter elements with transforms, and paint exotic effects with gradients, tiles, and images. Chapter 13: Geometries and Drawings delves deeper into 2-D drawing. You’ll learn to create complex paths that incorporate arcs and curves and how to use complex graphics efficiently. Chapter 14: Effects and Visuals describes lower-level graphics programming. You’ll apply Photoshop-style effects with pixel shaders, build a bitmap by hand, and use WPF’s visual layer for optimized drawing. Chapter 15: Animation Basics explores WPF’s animation framework, which lets you integrate dynamic effects into your application using straightforward, declarative markup. Chapter 16: Advanced Animations explore more sophisticated animation techniques like key-frame animation, path-based animation, and frame-based animation. You’ll also consider a detailed example that shows how to create and manage dynamic animations with code. Chapter 17: Control Templates shows you how you can give any WPF control a dramatic new look (and new behavior) by plugging in a customized template. You’ll also see how templates allow you to build a skinnable application. Chapter 18: Custom Elements explores how you can extend the existing WPF controls and create your own. You’ll see several examples, including a template-based color picker, a flippable panel, a custom layout container, and a decorator that performs custom drawing. Chapter 19: Data Binding shows you how to fetch information from a database, insert it into a custom data objects, and bind these objects to WPF controls. You’ll also learn how to improve the performance of huge data-bound lists with virtualization, and catch editing mistakes with validation. Chapter 20: Formatting Bound Data shows some of the tricks for turning raw data into rich data displays that incorporate pictures, controls, and selection effects. Chapter 21: Data Views explores how you use the view in a data-bound window to navigate through a list of data items, and to apply filtering, sorting, and grouping. Chapter 22: Lists, Grids, and Trees gives you a tour of WPF’s rich data controls, including the ListView, TreeView, and DataGrid. Chapter 23: Windows examines how windows work in WPF. You’ll also learn how to create irregularly shaped windows and use Vista glass effects. You’ll also make the most of Windows 7 features by customizing taskbar jump lists, thumbnails, and icon overlays. Chapter 24: Pages and Navigation describes how you can build pages in WPF and keep track of navigation history. You’ll also see how to build a browser-hosted WPF application that can be launched from a website. Chapter 25: Menus, Toolbars, and Ribbons considers command-oriented controls such as menus and toolbars. You’ll also get a taste of more modern user interface with the freely downloadable Ribbon control. Chapter 26: Sound and Video describes WPF’s media support. You’ll see how to control playback for sound and video, and how to throw in synchronized animations and live effects. Chapter 27: 3-D Drawing explores the support for drawing 3-D shapes in WPF. You’ll learn how to create, transform, and animate 3-D objects. You’ll even see how to place interactive 2-D controls on 3-D surfaces. xxxix www.it-ebooks.info ■ IntroduCtIon Chapter 28: Documents introduces WPF’s rich document support. You’ll learn to use flow documents to present large amounts of text in the most readable way possible, and you’ll use fixed documents to show print-ready pages. You’ll even use the RichTextBox to provide document editing. Chapter 29: Printing demonstrates WPF’s printing model, which lets you draw text and shapes in a print document. You’ll also learn how to manage page settings and print queues. Chapter 30: Interacting with Windows Forms examines how you can combine WPF and Windows Forms content in the same application—and even in the same window. Chapter 31: Multithreading describes how to create responsive WPF applications that perform time-consuming work in the background. Chapter 32: The Add-In Model shows you how to create an extensible application that can dynamically discover and load separate components. Chapter 33: ClickOnce Deployment shows how you can deploy WPF applications using the ClickOnce setup model. run a WPF 4.5 application, your computer must have Windows 7, Windows 8, or Windows Vista with create a WPF 4.5 application (and open the sample There’s one other option. Instead of using any version of Visual Studio, you can use Expression Blend—a application programmers. This book assumes you’re using Visual Studio. If you do plan to use Expression Blend, make sure you are using a version that explicitly supports WPF (the version that’s bundled with some versions of Visual Studio is for Metro development only, and doesn’t support WPF). At the time of this writing, the version of Expression Blend that supports WPF is available as a preview called Blend + Sketchflow Preview for Visual Studio 2012, and it’s available at http://tinyurl.com/cgar5lz. Code Samples and URLs It’s a good idea to check the Apress website or http://www.prosetech.com to download the most recent up-to-date code samples. You’ll need to do this to test most of the more sophisticated code examples described in this book because the less significant details are usually left out. This book focuses on the most important sections so that you don’t need to wade through needless extra pages to understand a concept. To download the source code, surf to http://www.prosetech.com and look for the page for this book. You’ll also find a list of links that are mentioned in this book, so you can find important tools and examples without needless typing. Feedback This book has the ambitious goal of being the best tutorial and reference for programming WPF. Toward that end, your comments and suggestions are extremely helpful. You can send complaints, adulation, and everything in between directly to [email protected]. I can’t solve your .NET problems or critique your code, but I will benefit from information about what this book did right and wrong (or what it may have done in an utterly confusing way). xl www.it-ebooks.info PART 1 Fundamentals www.it-ebooks.info ChAPTeR 1 Introducing WPF The Windows Presentation Foundation (WPF) is a modern graphical display system for Windows. It’s a radical change from the technologies that came before it, with innovative features such as built-in hardware acceleration and resolution independence, both of which you’ll explore in this chapter. WPF is the best toolkit to use if you want to build a rich desktop application that runs on Windows Vista, Windows 7, and Windows 8 in desktop mode (as well as the corresponding versions of Windows Server). In fact, it’s the only general-purpose toolkit that targets these versions of Windows. By comparison, Microsoft’s new Metro toolkit—although exciting—is limited to Windows 8 systems only. (WPF applications can even be made to run on ancient Windows XP computers, which are still found in many businesses. The only limitation is that you must configure Visual Studio to target the slightly older .NET 4.0 Framework, rather than .NET 4.5.) In this chapter, you’ll take your first look at the architecture of WPF. You’ll learn how it deals with varying screen resolutions, and you’ll get a high-level survey of its core assemblies and classes. You’ll also consider how WPF has evolved from its initial release to version 4.5. The Evolution of Windows Graphics Before WPF, Windows developers spent nearly 15 years using essentially the same display technology. That’s because every traditional, pre-WPF Windows application relies on two well-worn parts of the Windows operating system to create its user interface: • User32: This provides the traditional Windows look and feel for elements such as windows, buttons, text boxes, and so on. • GDI/GDI+: This provides drawing support for rendering shapes, text, and images at the cost of additional complexity (and often lackluster performance). Over the years, both technologies have been refined, and the APIs that developers use to interact with them have changed dramatically. But whether you’re crafting an application with .NET and Windows Forms or even Visual Basic 6 or MFC-based C++ code, behind the scenes the same parts of the Windows operating system are at work. Different frameworks simply provide different wrappers for interacting with User32 and GDI/GDI+. They can provide improvements in efficiency, reduce complexity, and add prebaked features so you don’t have to code them yourself; but they can’t remove the fundamental limitations of a system component that was designed more than a decade ago. ■ Note The basic division of labor between User32 and GDI/GDI+ was introduced more than 15 years ago and was well established in Windows 3.0. Of course, User32 was simply User at that point, because software hadn’t yet entered the 32-bit world. 3 www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.