1. Trang chủ
  2. » Công Nghệ Thông Tin

pro wpf 4.5 in vb

1,1K 4,4K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 1.078
Dung lượng 22,04 MB

Nội dung

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 v 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 www.it-ebooks.info ■ Contents at a GlanCe vi 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 www.it-ebooks.info xxxvii Introduction When .NET rst appeared, it introduced a small avalanche of new technologies. ere 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 signicantly, 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 dierent 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 eect behind a button) to spread into the area owned by another control. And don’t even think about introducing animated eects such as spinning text, shimmering buttons, shrinking windows, or live previews because you’ll have to paint every detail by hand. e Windows Presentation Foundation (WPF) changed all this by introducing a model with entirely dierent plumbing. Although WPF includes the standard controls you’re familiar with, it draws every text, border, and background ll 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. is means that you can use rich graphical eects without incurring the performance overhead that you’d suer with Windows Forms. In fact, you even get advanced features such as support for video les and 3-D content. Using these features (and a good design tool), it’s possible to create eye-popping user interfaces and visual eects 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. www.it-ebooks.info ■ IntroduCtIon xxxviii About This Book is 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. is book provides a complete description of every major WPF feature, from XAML (the markup language used to dene 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. ese features aren’t discussed here. Instead, if you want more information about .NET features that aren’t specic to WPF, you can refer to one of the many dedicated .NET titles from Apress. e 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 dene user interfaces. You’ll learn why it was created and how it works, and you’ll create a basic WPF window using dierent coding approaches. Chapter 3: Layout delves into the layout panels that allow you to organize elements in a WPF window. You’ll consider dierent 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: e 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 les 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. www.it-ebooks.info ■ IntroduCtIon xxxix 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 eects 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 eciently. Chapter 14: Eects and Visuals describes lower-level graphics programming. You’ll apply Photoshop-style eects 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 eects 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 ippable 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 eects. 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 ltering, 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 eects. 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 eects. 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. www.it-ebooks.info ■ IntroduCtIon xl Chapter 28: Documents introduces WPF’s rich document support. You’ll learn to use ow documents to present large amounts of text in the most readable way possible, and you’ll use xed 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: e 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 ere’s one other option. Instead of using any version of Visual Studio, you can use Expression Blend—a application programmers. is 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 + Sketchow 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 signicant details are usually left out. is 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 nd a list of links that are mentioned in this book, so you can nd important tools and examples without needless typing. Feedback is 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 apress@prosetech.com. I can’t solve your .NET problems or critique your code, but I will benet from information about what this book did right and wrong (or what it may have done in an utterly confusing way). www.it-ebooks.info PART 1 Fundamentals www.it-ebooks.info 3 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. www.it-ebooks.info [...]... window should be? The short answer is that WPF uses the WPF Units A WPF window and all the elements inside it are measured using device-independent units A single device-independent unit is defined as 1/96 of an inch To understand what this means in practice, you’ll need to consider an example Imagine that you create a small button in WPF that’s 96 by 96 units in size If you’re using the standard Windows... it would be a compelling improvement but a limited one But WPF includes a basket of high-level services designed for application programmers 4 www.it-ebooks.info CHAPTER 1 ■ InTRODUCInG WPF The following are some of the most dramatic changes that WPF ushers into the Windows programming world: • A web-like layout model: Rather than fix controls in place with specific coordinates, WPF emphasizes flexible... button in a window Chapter 5 has more about this technique Writing data-binding expressions: Data binding allows you to extract data from an object and display it in a linked element To set up this relationship and configure how it works, you must add a data-binding expression to your XAML markup Chapter 8 introduces data binding Defining resources: Resources are objects that you define once in a special... layout) By deriving from DispatcherObject, every element in your user interface can verify whether code is running on the correct thread and access the dispatcher to marshal code to the user interface thread You’ll learn more about the WPF threading model in Chapter 31 12 www.it-ebooks.info CHAPTER 1 ■ Introducing WPF System.Windows.DependencyObject In WPF, the central way of interacting with onscreen... advantage of WPF s rendering engine • WindowsBase.dll: This holds even more basic ingredients that have the potential to be reused outside of WPF, such as DispatcherObject and DependencyObject, which introduces the plumbing for dependency properties (a topic you’ll explore in detail in Chapter 4) 10 www.it-ebooks.info CHAPTER 1 ■ Introducing WPF • milcore.dll: This is the core of the WPF rendering system... can follow Looking at the start tag for the Window element, you’ll find several interesting attributes, including a class name and two XML namespaces (described in the following sections) You’ll also find the three properties shown here: 4 Title="Window1" Height="300" Width="300"> Each attribute corresponds to a separate property of the Window class All in all, this tells WPF to create a window with the... also find links to other Microsoft-managed WPF projects, including WPF Futures (which provides more experimental WPF features) and WPF testing tools Visual Studio 2012 Although you can craft WPF user interfaces by hand or using the graphic-design-oriented tool Expression Blend, most developers will start in Visual Studio and spend most (or all) of their time there This book assumes you’re using Visual... from worrying about releasing objects and reclaiming memory, milcore.dll saves you from thinking about invalidating and repainting a window You simply create the objects with the content you want to show, and milcore.dll paints the appropriate portions of the window as it is dragged around, covered and uncovered, minimized and restored, and so on • WindowsCodecs.dll: This is a low-level API that provides... clearly defined edges if you use custom-drawn graphics to skin them; so some level of anti-aliasing is already necessary 7 www.it-ebooks.info CHAPTER 1 ■ Introducing WPF The steps for adjusting the system DPI depend on the operating system The following sections explain what to do, depending on your operating system Windows Vista 1 Right-click your desktop and choose Personalize 2 In the list of links... www.it-ebooks.info CHAPTER 1 ■ Introducing WPF ■■Note  In Windows Forms programming, every visual item in a form is referred to as a control In WPF, this isn’t the case Visual items are called elements, and only some elements are actually controls (those that can receive focus and interact with the user) To make this system even more confusing, many elements are defined in the System.Windows Controls namespace, . 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. classes of WPF www.it-ebooks.info CHAPTER 1 ■ INTRODUCING WPF 13 System.Windows.DependencyObject In WPF, the central way of interacting with onscreen elements is through properties. Early on in the. compelling improvement but a limited one. But WPF includes a basket of high-level services designed for application programmers. www.it-ebooks.info CHAPTER 1 ■ INTRODUCING WPF 5 The following are

Ngày đăng: 05/05/2014, 12:10

Xem thêm

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN