WPF - Introduction

Page 1

1


2


Windows Presentation Foundation (WPF) is a graphical sub system for creating windows application with visually compelling user experiences that incorporates media, documents, animation , graphics, imaging etc. A new presentation programming model that provides the foundation for building the next generation User Interface. It has the capabilities of preceding technologies such as MFC, Windows Forms including GDI, GDI+, HTML, and much more. It combines together Application UI, Documents, 2D and 3D Graphics, Animation and Media. It also exploits the power of 3D Graphic Card by using DirectX in its core. History of Rich Client applications: The Windows API exposes the core set of functionality provided by the Windows operating system for use in applications. Primarily designed for C/C++ development, the Windows API has been the most direct mechanism with which an application can interact with Windows. The Windows API comprises the following functional groups: Base Services—Provides access to the core resources of the computer, such as memory, file systems, devices, processes, and threads Common Control Library—A collection of common control windows used throughout the Windows operating system, providing the distinctive Windows look and feel Common Dialog Box Library—A collection of dialog boxes used to execute common tasks, including file opening, saving, and printing Graphics Device Interface (GDI)—Provides the facilities for an application to generate graphical output to displays, printers, and other devices User Interface (UI)—Provides the mechanism for managing windows and controls in an application and input from devices such as the mouse and keyboard Windows Shell—The container that organizes and presents the entire Windows UI, including the desktop, taskbar, and Windows Explorer Through these services, developers have had significant flexibility in creating powerful applications for the Windows operating system. However, this flexibility also bore the responsibility of handling low level and often tedious operations. With each new release of the Windows operating system, additions and updates to the Windows API were almost always included. Yet with each release, Microsoft strived to support backwards compatibility. Thus, many functions included in the original Windows API still exist today in Windows XP and Windows Server 2003. Since the release of Windows 1.0 more than 20 years ago, the GDI and the UI services of the Windows API have provided a reliable graphics platform for Windows applications. Many applications we use on a day-to-day basis, including the Microsoft Office suite and Internet Explorer, are built on this foundation. Although this graphics platform has seen many successes, fundamental changes have occurred in technology since its initial appearance more than two decades ago. Wide use of the personal computer and the increasing availability of broadband Internet access, the demand for rich visual experiences has dramatically increased. Moreover, advancements in graphics hardware technology made it possible to create applications that incorporate digital media for interactive entertainment products. Facing this new demand for a rich visual experience, Microsoft has invested heavily in providing such an experience as part of the next-generation Windows API—.NET Framework 3.0. Formerly known as Avalon, Windows Presentation Foundation (WPF) is the new graphical subsystem Was introduced in .NET Framework version 3.0 It combines user interface, 2D and 3D graphics, documents, and digital media. Built on the .NET Framework, WPF provides a managed environment for development with the Windows operating system. This takes advantage of the existing investment made by Microsoft in the .NET Framework, and allows developers familiar with .NET technologies to rapidly begin developing applications that leverage WPF. The developers could also create rich client application using C# Win forms. But these applications provided a limited set of features like controls with no support to customize their look, 2-D graphics only, data binding , Modal windows, Dialogs . But there was no support for stylizing , customizing the look of the controls or the data that is displayed in the controls. No support for media services like animations (Note : .NET Framework 3.0 is a revolutionary milestone for developing applications on the Windows operating system. Built atop the .NET Framework 2.0, .NET Framework 3.0 is a set of managed APIs that provide enhanced functionality for messaging(WCF), workflow(WF), and presentation(WPF). These were further enhanced in next version like .NET Framework 3.5 and .NET Framework 4.0 ) Features: 1)DirectX Microsoft DirectX is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. WPF uses Direc3D a variant of DirectX for rendering graphics like 3-D drawings, transparency and anti-aliasing. Direct3D integration allows for streamlined 3D Graphics. In today’s world, developing a Windows application may require the use of any number of different technologies, ranging from GDI/GDI+ for 2D graphics, UI services (User32 or WinForms), or Direct3D for 3D graphics. On the contrary, WPF was designed as a single model for application development, providing seamless integration between such services within an application. Similar constructs can be used for developing storyboard animation, data bound forms, and 3D models. (Note : Details on DirectX API is beyond the scope of this topics) 2)Vector Graphics and Media Services: Vector Graphics To take advantage of new powerful graphics hardware, WPF implements a vector-based composition engine. This allows for graphics to scale based on screen-specific resolution without loss of quality, something nearly impossible with fixed-size raster graphics. WPF leverages Direct3D for vector-based rendering, and will utilize the graphics processing unit (GPU) on any video card implementing DirectX. Media Services The WPF provides an integrated system for building user interfaces with common media elements like vector and raster images, audio, and video. WPF also provides an animation system and a 2D/3D rendering system. WPF provides shape primitives for 2D graphics along with a built-in set of brushes, pens, geometries, and transforms. The 3D capabilities in WPF are a subset of the full-feature set provided by Direct3D. However, WPF provides tighter integration with other features like user interfaces, documents, and media. This makes it possible to have 3D user interfaces, 3D documents, or 3D media. There is support for most common image formats: BMP, JPEG, PNG, TIFF, Windows Media Photo, GIF, and ICON. WPF supports the video formats WMV, MPEG and some AVI files by default, but since it has Windows Media Player running beneath, WPF can use all the codecs installed for it. Animations: WPF supports time-based animations. WPF supports low level animation via timers and higher level abstractions of animations via the Animation classes. Any WPF element property can be animated as long as it is registered as a Dependency Property. Animation classes are based on the .NET type of property to be animated. For instance, changing the color of an element is done with the ColorAnimation class and animating the Width of an element (which is typed as a Double) is done with the DoubleAnimation class. Animations can be grouped into Storyboards. Storyboards are the primary way to manage animations such as start, stop, pause and otherwise manipulate the animations. Animations can be triggered by external events, including user action. Animation effects can be defined on a per-object basis, which can be accessed directly from XAML markup. 3)Allows Declarative Programming WPF introduces a new XML-based language to represent UI and user interaction, known as XAML (Extensible Application Markup Language—pronounced “zammel”). Since it is a derivative of XML, within XAML, elements from the UI are represented as XML tags. Using XAML, WPF provides a clear separation between the user interface and the business logic. Thus, XAML allows applications to dynamically parse and manipulate UI elements at either compile-time or runtime, providing a flexible model for UI composition. XAML follows the code-behind model, allowing designers and developers to work in parallel and seamlessly combine their work to create visually stunning user experiences. 4)Document Services: Natively supports XML Paper Specification documents(XPS). Similar to the Portable Document Format (PDF), the XML Paper Specification (XPS), allows for documents to be shared across computers without requiring that the originating application be installed. 5)Data Binding: WPF has a built-in set of data services to enable application developers to bind and manipulate data within applications. It supports binding data using ADO. Net objects, LINQ Queries, XML data To Create WPF application , use Visual Studio.NET WPF Application Project template that reference PresentationFramweork.dll and PresentationCore.dll and System.Windows.

3


Designer and Developer Collaboration The success of the XML as a markup language can be gauged from the fact that today there are more than 250 derivatives of XML available and are used. Following the markup language for the web ,WPF introduces a new language known as Extensible Application Markup Language (XAML) pronounced as ‘zammel’, which is a derivative of XML. XAML is designed as a more efficient method of developing application user interfaces. XAML allows to create visible UI elements in the declarative XAML markup, and then separate the design logic from the business logic by using code-behind files, linked to the markup through partial class definitions in code behind file. In XAML, elements and attributes map to classes and properties in the underlying Managed API’s. Microsoft XAML design tools: · Microsoft Expression Design: This tool is aimed at designers and artists who require a powerful drawing tool. All art created in Expression Design can be exported as XAML, making it easy for a designer to hand off artwork to WPF developers. ·Microsoft Expression Blend: This tool is used to create WPF UI presentation and behavior. Blend integrates with Expression Design and Visual Studio to blend together a full WPF design/development solution. ·WPF Designer: This is part of Visual Studio 2008 and lets you create the UI, via a graphical user interface or by editing XAML, in the same tool that you use to write your application. ·XAMLPad: A lightweight simple editor that shows the results of XAML as you type. Graphics WPF introduces a extensive set of graphics features that have the following benefits: i)Resolution-independent and device-independent graphics. The basic unit of measurement in the WPF graphics system is the device independent pixel, which is 1/96th of an inch, regardless of actual screen resolution, and provides the foundation for resolution-independent and device-independent rendering. Each device-independent pixel automatically scales to match the dots-per-inch (dpi) setting of the system it renders on. ii)Improved precision. The WPF coordinate system is measured with double-precision floating-point numbers rather than single-precision. Transformations and opacity values are also expressed as double-precision. iii) Animation Prior to WPF, Microsoft Windows developers had to create and manage their own timing systems or use special custom libraries. WPF includes an efficient timing system that is exposed through managed code and Extensible Application Markup Language (XAML) and that is deeply integrated into the WPF framework. WPF animation makes it easy to animate controls and other graphical objects. iii)Hardware acceleration. The WPF graphics system takes advantage of graphics hardware to minimize CPU usage. (Note : Details on working with XAML will be covered in XAML Session)

4


Controls WPF provides wide variety of control for creating rich user experiences, as listed below, The built-in WPF controls are listed here. Buttons: Button and RepeatButton. Dialog Boxes: OpenFileDialog, PrintDialog, and SaveFileDialog , MessageBox etc. Documents: DocumentViewer, FlowDocumentPageViewer, FlowDocumentReader, FlowDocumentScrollViewer, and StickyNoteControl. Input Controls: TextBox, RichTextBox, and PasswordBox. Layout: Border, BulletDecorator, Canvas, DockPanel, Expander, Grid, GridView, GridSplitter, GroupBox, Panel, ScrollBar, ScrollViewer, StackPanel, Thumb, Viewbox, VirtualizingStackPanel, Window, and WrapPanel. Media Services: Image, MediaElement, and SoundPlayerAction. Menus and toolbars : ContextMenu, Menu, and ToolBar. Navigation: Frame, Hyperlink, Page, NavigationWindow, and TabControl. List Controls: CheckBox, ComboBox, ListBox, TreeView, RadioButton, and Slider. User Information: AccessText, Label, Popup, ProgressBar, StatusBar, TextBlock, and ToolTip.

5


Data Binding A Data binding is a relationship that tells WPF to extract some information from a source of data and use it to set a property in a target object that would be used to display data. The target property is always a dependency property. However, the source object can be just about anything, ranging from another WPF element to an ADO.NET data object (like the DataTable and DataRow) or a user defined data-only object . OneWay – changes to the source property automatically update the target property, but not vice versa TwoWay – changes to either the source property or the target property automatically update the other OneWayToSource – The opposite of the OneWay. (Note : Detailed information on WPF Data Binding will be covered in session WPF Data Binding session).

6


WPF standalone and Browser based applications. WPF supports creating Standalone and Browser based applications. 1)Standalone Applications: Users interact with Windows Presentation Foundation (WPF) standalone applications through windows. The primary purpose of a window is to host content that visualizes data and enables users to interact with data. Standalone WPF applications provide their own windows by using the Window class. Developers use Window class to create standalone applications that make use of windows and dialog boxes that are accessed from menu bars and tool bars. 2)Browser Based Applications: It encapsulates a page of content that can be navigated to and hosted by Windows Internet Explorer, NavigationWindow, and Frame. WPF offers the two following options for alternative navigation hosts: 1) Frame , to host navigable content in either pages or windows. 2) NavigationWindow , to host navigable content in an entire window. Note :( Detailed explanation on XAML browser application is beyond the cope of this session. For detailed reading use MSDN link)

7


Documents: WPF offers a wide range of document features that enable the creation of high-fidelity content that is designed to be more easily accessed and read than in previous generations of Windows. WPF also provides integrated services for document display, packaging, and security. WPF natively supports working with three types of documents: 1)Flow documents, 2)Fixed documents, and 3) XML Paper Specification (XPS) documents. WPF also provides the document services such as create, view, manage, annotate, package, and print documents. Flow Documents : Flow documents provide optimized viewing and readability by dynamically adjusting and reflowing content when window size and display settings are changed. That is a flow document is designed to reflow content depending on window size, device resolution, and other environment variables. In addition, flow documents have a number of built in features including search, viewing modes that optimize readability, and the ability to change the size and appearance of fonts. Flow Documents are best utilized when ease of reading is the primary document consumption scenario. In contrast, Fixed Documents are designed to have a static presentation. Fixed Documents are useful when fidelity of the source content is essential. Fixed Documents : Fixed documents are intended for applications that require a precise presentation, independent of the display or printer hardware used. As part of its layout, a fixed document maintains the precise positional placement of content elements independent of the display or print device in use. Fixed documents maintain the precise arrangement of their content in a device-independent manner. For example, a fixed document that is displayed on a 96 dots-per-inch (dpi) display appears the same as when it is printed to either a 600 dpi laser printer or a 4800 dpi photo typesetter. The layout remains the same in all cases, although the document's quality varies depending on the capabilities of each device. Fixed documents are intended for applications that require a precise "what you see is what you get" (WYSIWYG) presentation, with regards to printing. Typical uses for fixed documents include desktop publishing, word processing, and form layout. XPS Documents Electronic documents is a way to publish and exchange information over the wire. But there are certain drawbacks of these papers like : The software that reads the document, documents often appear one way when viewed on the screen and another when printed. Mostly many documents depend on locally installed software, fonts, and other resources. When a document is viewed on different computers, aspects of the document, such as page layout, image color, and font style, can change, altering the document's appearance. Consequently, a document's appearance depends greatly on device and how the document is viewed. The appearance of the printed version of a document can also vary, depending on the printer used. Unfortunately, there is currently no guarantee that users with different printers attached to different machines will see the exact same document printed on paper as when the document is viewed on the monitor screen. In fact, it is almost guaranteed that each version of the document will have a different appearance. Microsoft provides a solution to address these problems with an improved document technology – XPS Documents. XML Paper Specification (XPS) documents build on WPF's fixed documents. It is an electronic paper, described with an XML-based schema. XPS is an open, cross-platform document format that is designed to facilitate the creation, sharing, printing, and archiving of paginated documents. These documents can be read by humans, any software application that supports XML and any hardware. (Note :-Detailed explanation on XAML browser application is beyond the cope of this session. For detailed reading use MSDN link)

8


2-D graphics: A subset of WPF 2-D capabilities includes visual effects, such as gradients, bitmaps, drawings, painting with videos, rotation, scaling, and skewing. These are all achieved with brushes;

3-D graphics: The WPF 3-D implementation allows developers to draw, transform, and animate 3-D graphics in both markup and procedural code by using classes like ViewPort 3-D, Camera , MeshGeometry3D class etc (Note: Detailed explanation for these graphics classes is beyond the scope of this session.) Imaging: There are several ways to display an image in a Windows Presentation Foundation (WPF) application. Images can be displayed using an Image control, painted on a visual using an ImageBrush, or drawn using an ImageDrawing. The majority of the managed WPF Imaging API reside in the System.Windows.Media.Imaging namespace, though several important types, such as ImageBrush and ImageDrawing reside in the System.Windows.Media namespace and Image resides in the System.Windows.Controls namespace. (Note: Detailed explanation for these imaging classes is beyond the scope of this session. For detailed reading use MSDN link)

9


Animations: WPF supports extensive support for creating animated application by providing animation classes from System.Windows.Media.Animation namespace. Prior to WPF, Microsoft Windows developers had to create and manage their own timing systems or use special custom libraries. WPF includes an efficient timing system that is exposed through managed code and Extensible Application Markup Language (XAML) seamlessly integrates with the WPF framework. WPF animation classes makes it easy to animate controls and other graphical objects. WPF handles all the behind-the-scenes work of managing a timing system and redrawing the screen efficiently. It provides timing classes that enables developers to focus only on the effects to be created, instead of the writing the logic necessary for behind the scene mechanism for achieving those effects. WPF also customizing animations by exposing an animation base classes to achieve in many of the performance benefits of the standard animation classes. Support for audio and video media : WPF is capable of playing both video and audio files. The MediaElement control is capable of playing both video and audio files. It can also play multiple media files. ( Detailed explanation on WPF Animation's is beyond the cope of this session. For detailed reading use MSDN link)

10


Styles: These are used to standardize the look of WPF form elements. A WPF Style is a convenient way to apply a set of property values to more than one element. Layouts:

Canvas : Child controls provide their own layout. Grid : Child controls are positioned in tabular form as rows and columns. DockPanel : Child controls are docked to the edges of the panel like Top, Bottom, Left and Right. StackPanel : Child controls are stacked either vertically or horizontally. WrapPanel : Child controls are positioned in left-to-right order and wrapped to the next line when there are more controls on the current line than space allows like that of a note book. Templates: A control template lets you specify the appearance of a control while a data template lets you specify the appearance of a control's content. Example : WPF allows to create a Button that is elliptical in shape by using Control templates. A simple List Box allows to display data in list box but the dame data is to be displayed in the form of bands of data, using Data Templates. (Note : Detailed information on WPF layouts and Styles will be covered in session WPF Layouts and WPF Resources session respectively. For detailed reading use MSDN link).

11


Interoperability WPF provides interoperability. That is WPF application permit hosting Windows Forms Controls in WPF using WindowsFormsHost . A Windows Forms can host a Windows Presentation Foundation (WPF) element by using ElementHost class. (Note : Detailed information on WPF Interoperability will be covered in session WPF Windows session). How WPF achieves Interoperability? 1) Use the ElementHost control to place a WPF UIElement on your Windows Forms control or form. 2) To host a Windows Forms control in a WPF element, use the WindowsFormsHost element. Typographic Features: ClearType ClearType is Microosft’s software technology to improves the readability of text on existing LCDs (Liquid Crystal Displays), such as laptop screens, Pocket PC screens and flat panel monitors. It works by accessing the individual vertical color stripe elements in every pixel of an LCD screen. Prior to ClearType, the smallest level of detail that a computer could display was a single pixel, but with ClearType running on an LCD monitor, we can now display features of text as small as a fraction of a pixel in width. The extra resolution increases the sharpness of the tiny details in text display, making it much easier to read over long durations. The ClearType available in Windows Presentation Foundation (WPF) is the latest generation of ClearType which has better features than those found in Microsoft Windows Graphics Device Interface (GDI). Enhanced font Support It supports unicode for all text. Font behavior and selection no longer require charset or codepage. Font behavior independent of global settings, such as system locale. It provides separate FontWeight, FontStretch, and FontStyle types for defining a FontFamily. Writing direction (horizontal versus vertical) handled independent of font name. Enhanced International Text Automatic line-spacing in all writing systems, using adaptive measurement. Broad support for international text. Language-guided line breaking, hyphenation, and justification New text application programming interfaces (APIs). WPF provides several text APIs for developers to use when including text in their applications. These APIs are grouped into three categories: a) Layout and user interface The common text controls for the graphical user interface (GUI). b) Lightweight text drawing. Allows a developer to draw text directly to objects. c) Advanced text formatting. Allows you to implement a custom text engine. ( Detailed explanation on WPF Interoperability will be covered in WPF Windows session. Detailed explanation WPF Typography features is beyond the cope of this session. For detailed reading use MSDN link) For detailed reading use MSDN link)

12


The MediaElement class from System.Windows.Controls namespace is capable of playing both video and audio. WPF helps creating rich content is through the use of audiovisual media by supporting features like images, video, and audio. (Note : Details on using Multimedia element is beyond the scope of this topic. For detailed reading use MSDN link)

13


The architecture of WPF spans across both managed code and native code components. The components of WPF architecture are 1) Managed API Layer : It comprises of a) PresentationFramework b) PresentationCore and 3) Milcore and 4) WindowsBase a) PresentationFramework : PresentationFramework (presentationframework.dll) implements the end-user UI features like various layout container to organize controls on WPF form, animations that are time-dependent and story-board and data binding –Supports binding to in- memory collections, ADO.net objects and XML data . b)PresentationCore (presentationcore.dll) It is a managed wrapper over MilCore.dll. It includes the base types, such as UIElement and Visual, from which all shapes and controls derive. It provides a managed wrapper for MIL and implements the core services for WPF, including a property system that is aware of the dependencies between the setters and consumers of the property, a message dispatching system by means of a Dispatcher object to implement a specialized event system and services which can implement a layout system such as measurement for UI elements. c)WindowsBase.dll It 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 The DispatcherObject class provides the basic constructs for dealing with threading. WPF exposes a property system for objects which inherit from DependencyObject, that is aware of the dependencies between the consumers of the property, and can trigger actions based on changes in properties. Properties can be either hard coded values or expressions, which are specific expressions that evaluate to a result. 2) Native API layer a)MilCore - : It is the core of the WPF rendering system and the foundation of the Media Integration Layer (MIL). Its composition engine renders visual elements into a form that Direct3D expects. While the majority of WPF is in managed code, the composition engine which renders the WPF applications is a native component. It is named as Media Integration Layer (MIL) and available in milcore.dll. It interfaces directly with DirectX and provides basic support for 2D and 3D surfaces, timer-controlled manipulation of contents of a surface with a view to exposing animation constructs at a higher level, and compositing the individual elements of a WPF application into a final 3D "scene" that represents the UI of the application and renders it to the screen. b) WindowsCodecs.dll It is a low-level API that provides imaging support (for example, processing, displaying, and scaling bitmaps and JPEGs). c)DirectX DirectX is a set of low-level APIs for creating games and other high-performance multimedia applications. It includes support for high-performance 2D and 3D graphics, sound, and input. It is the low-level API that renders all the drawing in WPF. d) User32 It is a Win32 API. It implements the Windows USER component that creates and manipulates the standard elements of the Windows user interface, such as the desktop, windows, and menus. It thus enables programs to implement a graphical user interface that matches the Windows look and feel. Programs call functions from Windows USER to perform operations such as creating and managing windows, receiving window messages (which are mostly user input such as mouse and keyboard events, but also notifications from the operating system), displaying text in a window, and displaying message boxes. Many of the functions in User32.dll call upon GDI functions exported by Gdi32.dll to do the actual rendering of the various elements of the user interface. Some types of programs will also call GDI functions directly to perform lower-level drawing operations within a window previously created via User32 functions. Though it is used in WPF, but it plays no part in rendering common controls. Summary: The major components of WPF are -PresentationFramework, -PresentationCore, and -Milcore unmanaged component Milcore is written in unmanaged code in order to enable tight integration with DirectX. All display in WPF is done through the DirectX engine, facilitating efficient hardware and software rendering. WPF also required fine control over memory and execution.

14


15


1)DispathcerObject: System.Threading.DispatcherObject All WPF applications start with two threads: one for managing the UI and another background thread for handling rendering and repainting. Rendering and repainting is managed by WPF itself, without any developer intervention. The UI thread owns the Dispatcher via an instance of DispatcherObject, which maintains a queue of UI operations that need to be performed as a tree of Visual objects, sorted by priority. UI events, including changing a property that affects the layout, and user interaction events raised are queued up in the dispatcher, which invokes the handlers for the events. The render thread picks up a copy of the visual tree (For each element in the WPF form element tree, additional elements may be created to represent visual aspects required by the element)and walks the tree calculating which components will be visible and renders them to Direct3D surfaces. The render thread also caches the visual tree, so only changes to the tree need to be communicated, which will result in updating only the changed pixels. In WPF, the entire user interface is owned by a single thread , i.e., Single threaded Affinity (STA) model. So to avoid its interaction with user interface elements from another thread, each WPF application is governed by a dispatcherObject. It coordinates messages that result from keyboard input, mouse movements, and framework processes such as 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. 2)DependencyObject : System.Windows.DependencyObject: WPF allows to interact with onscreen elements is through properties. WPF exposes a property system(dependency property system -DPS) for objects which inherit from DependencyObject . DPS provides a new set of property types and property services to objects that derive from DependencyObject. When an object derives from DependencyObject, it automatically has access to all of the services provided by the DPS. A dependency object may declare dependency properties. The Dependency Property class represents a dependency property that is registered with the WPF dependency property system. This property system tracks dependencies between property expressions and automatically updates property values when dependencies change. It is aware of the dependencies between the consumers of the property, and can trigger actions based on changes in properties. It is all about properties than methods or events. Properties are declarative and they specify intent instead of action. Properties can be either hard coded values or expressions, which are specific expressions that evaluate to a result. The value of the properties can be inherited from parent objects as well. WPF properties support change notifications, which invoke bound behaviors whenever some property of some element is changed. Custom behaviors can be used to propagate a property change notification across a set of WPF objects. This is used by the layout system to trigger a recalculation of the layout on property-changes, thus exposing a declarative programming style for WPF, whereby almost everything, from setting colors and positions to animating elements can be achieved by setting properties. This allows WPF applications to be written in XAML, which is a declarative mark-up language, by binding the keywords and attributes directly to WPF classes and properties. The DPS offers many advantages over plain CLR type properties. For instance, through the use of a new type of properties called dependency properties, the DPS supports property-value inheritance, which allows elements to inherit property values from any of their containing elements. Dependency properties also provide the glue required for data binding and animation. Additionally, the DPS supports another new type of property, called an attached property, which is a dependency property with a twist. Attached properties are interesting in that they provide a mechanism by which one class can define a property that can be used by other classes, without the other classes knowing anything about the property or the class that defines it. Examples of these can be found in many of the built-in layout controls to provide a means for child controls to position themselves in their parent containers. One such example is the DockPanel.Dock attached property. Additionally, the DPS provides a storage system for dependency property values and will notify objects when these values change. The .NET Framework INotifyPropertyChange interface provides a mechanism for an object to publish change notifications when the dependency changes. DependencyObject also supported data driven, system for displaying user interface content. They are best fit in a model driven or data driven architecture Dependency property are used in data binding. WPF also supports concept of attached properties. Example . To anchor a button to the top region of the WPF form an attribute like “DockPanel.Dock =Top” is used. Here the dock is the property that is defined in DockPanel class but is used with the Button element. 3)Visual -System.Windows.Media.Visual : The Visual class provides services and properties that are common to all visual objects. A visual is a single drawing object that encapsulate drawing information. Visual connects between WPF composition model and milcore. The Visual class provides for building a tree of visual objects, each optionally containing drawing instructions and metadata about how to render those instructions (clipping, transformation, Opacity etc.). All the UI elements of a WPF application are maintained as a class of Visual objects. WPF displays data by traversing the unmanaged data structures managed by the milcore. These structures, called composition nodes, represent a hierarchical display tree with rendering instructions at each node. Thus Visual objects provide a managed interface to a composition tree which is maintained by Media Integration Layer (MIL). Each element of WPF creates and adds one or more composition nodes to the tree. The composition nodes contain rendering instructions, such as clipping, opacity or transformation instructions, along with other visual attributes. Thus the entire application is represented as a collection of composition nodes, that are cached. Periodically, MIL walks the tree and executes the rendering instructions in each node, thus compositing each element on to a DirectX surface, which is then rendered on screen. MIL uses the painter's algorithm, where all the components are rendered from back of the screen to the front, which allows complex effects like transparencies to be easily achieved. This rendering process is hardware accelerated using the GPU(graphical processing Unit). The composition tree is cached by MIL, creating a retained mode graphics, so that any changes to the composition tree needs only to be incrementally communicated to MIL. This also frees the applications of managing repainting the screen; MIL can do that itself as it has all the information necessary. Animations can be implemented as time-triggered changes to the composition tree. Developers specify animations declaratively, by setting some animation effect to some element via a property and specifying the duration. The code-behind updates the specific nodes of the tree, via Visual objects, to represent both the intermediate states at specified time intervals as well as the final state of the element. MIL will render the changes to the element automatically. Thus the Visual offers support for other features as well, such as transformations, clipping , opacity etc. Notice, however, that Visual does not provide support beyond the single element that derives from it. Non-rendering features, such as event routing and handling, layout, style, and data binding, are not supported at this level. They are supported by either UIElement or FrameworkElement, which derive from Visual Note :GPU :(graphics processing unit or GPU (also occasionally called visual processing unit or VPU) is a specialized circuit designed to rapidly manipulate and alter memory in such a way so as to accelerate the building of images in a frame buffer intended for output to a display. GPUs are used in embedded systems, mobile phones, personal computers, workstations, and game consoles. Modern GPUs are very efficient at manipulating computer graphics.) 4)UIElement -System.Windows.UIElement: UIElement adds support for WPF layout, Input and Event. The WPF layouts control the rendering behavior of the element and its child elements. UIElement provides layout to organize element on the WPF form , and also exposes virtual methods that derived classes can override, The UIElement class derives from Visual and marks the beginning of functionality that supports user interaction in WPF elements. In terms of elements, Visual represents a simple, non-interactive graphic. UIElement extends Visual by adding interactivity, such as, layout, input, focus, and events to an element. UIElement provides the definition for major interactive subsystems in WPF. UIElement provides two primary methods used in the WPF layout system, Measure and Arrange, and as such is the base class for elements that participate in the WPF layout system such as Panel. The final implementation of layout is defined by classes that derive from UIElement, such as FrameworkElement. Input acts as a signal on a kernel mode device driver and gets routed to the correct process and thread through the Windows kernel and User32. Once the User32 message corresponding to the input is routed to WPF, it is converted into a WPF raw input message and sent to the dispatcherObject. WPF allows for raw input events to be converted to multiple actual events, enabling features like MouseEnter or MouseLeave to be implemented at a low level of the system with guaranteed delivery. The UIElement class defines much of the input and focusing behavior for elements like the events for keyboard, mouse and stylus input, and related status properties. Example : Consider the following chain of events: 1. The user clicks his or her mouse. 2. User32 receives the device input message. 3. WPF receives the raw message and converts it into an input report (a WPF wrapper surrounding device input). 4. The application distinguishes the type of input report received from WPF and performs structural and geometric hit tests to determine the click target. 5. After validating the click target, the input report is converted into one or more events. For each event discovered, an event route is created, based on the composition of the hit area. 6. Each discovered event is raised by the input/event system. WPF is responsible for receiving the device input signal and determining the appropriate course of action based on the type of input received and events exposed by elements within an application. Furthermore many of these events are routed events, and many of the input-related events have both a bubbling routing version as well as a tunneling version of the event. All events in WPF are routed through the element tree are either ‘bubbled’ or ‘tunneled’. What are "bubbled" events? If the events traverse from a target up the tree to the root. What are “tunneled" events? The events are said to "tunneled" if they start at the root and traverse down to a target. 5)FrameworkElement-System.Windows.FrameworkElement: All the classes defined until now are part of the presentation core. Presentation framework is built on top of presentation core and defines a class called FrameworkElement, which derives from UIElement. The WPF presentation framework really just wraps and extends the presentation core. All elements in WPF derive, directly or indirectly, from FrameworkElement, which in turn derives from UIElement. FrameworkElement extends UIElement by adding the following characteristics: extended layout, styles, and data binding. FrameworkElement extends the layout functionality provided by UIElement. Methods defined as in UIElement such as Measure, MeasureBase, Arrange, and ArrangeBase, are extended and implemented in FrameworkElement in order to specify WPF framework–specific features. FrameworkElement adds layout properties, such as Min/Max Width, Min/Max Height, Width, Height,AcutalWidth, ActualHeight, Margin, HorizontalAlignment, and VerticalAlignment to further control element layout. FrameworkElement provides support for data binding. FrameworkElement offers the DataContext property, which provides a way in which you can define a data source for binding purposes. When the DataContext property is set on an element, all children of the element also inherit the data context. This provides for greater flexibility at design time. FrameworkElement also provides a Resources property, which returns the associated resource dictionary. Resources are typically defined at the Window or Page level, but can also be defined at the element level, making the resource dictionary easy to find and access or bind to from an individual element. FrameworkElement is the link between WPF framework-level element classes and the WPF core-level set of UIElement presentation services as listed below a)Layout system : FrameworkElement provides specific WPF framework-level implementations for certain methods that were defined as virtual members in UIElement. Most notably, FrameworkElement seals certain WPF core-level layout overrides, and instead provides a WPF framework-level equivalent that derived classes should override instead. For example, FrameworkElement seals ArrangeCore but provides ArrangeOverride. These changes reflect the fact that at the WPF framework-level there is a full layout system in place that can render any FrameworkElement derived class. At the WPF core level, certain members that will structure a general WPF based layout solution are in place, but the actual engine of the layout system is not defined. b) Logical trees:The logical tree is the hierarchical structure containing the exact elements of your WPF application as defined either declaratively within a XAML file, or imperatively in code. c) Object lifetime events: It is often useful to know when an element is initialized (the constructor is called) or when the element is first loaded into a logical tree. FrameworkElement defines several events related to object lifetime that provide useful hooks for code-behind operations that involve elements, such as adding more child elements. d) Support for data binding and dynamic resource references: WPF supports data binding using DependencyObject property system. e)Styles: FrameworkElement defines the Style property. A Style is a convenient way to apply a set of property values to more than one element to give However, FrameworkElement does define support for templates like control template. These features are introduced by control classes such as Control and ContentControl. f)Animation support: WPF supports animations by implementing BeginStoryboard and related members. 6)Control: System.Windows.Controls.Control: The Control base class allows you to define behavior specific to a particular control. Control derives from and extends FrameworkElement by adding support for additional features, such as context menus, tooltips, and control templates, and by providing additional properties such as Background and Foreground. Using the Control base class, developer can define custom controls. Controls in WPF are referred to as look less, meaning they provide behavior separate from appearance. But WPF provide Control templates to change the default look and feel of a control. A control is an element that are used to design WPF forms and allow user to interaction with them. It includes classes like Button, radio button, menus, toolbars, Combo Box etc Control provides a set of stock properties like Background, Foreground, margin, height, Padding etc. The template authors can then use these to customize the look of a control by creating control templates. The implementation of a control provides a data model and interaction model. The interaction model defines a set of commands like Close for a window and bindings to input actions like clicking the close windows button(red X) in the upper corner of the window. The data model provides a set of properties to either customize the interaction model or customize the display determined by the template called data templates. 7)System.Windows.Controls.ContentControl: This is the base class for all controls that display single piece of content like Button control that has a content property to display the button caption. 8)System.Windows.Controls.ItemsControl: This is the base class for all controls that show a collection of items, such as the ListBox , ComboBox, ListView and TreeView , toolbars, and status bars etc. The WPF List controls differ from Windows Forms control as they highly customisable—for example, using the features that are built into the ItemsControl class can be used to transform the lookless ListBox into a list of radio buttons, a list of check boxes, a tiled display of images, or a combination of completely different elements.

16


17


18


19


20


21


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.