WPF – Windows Presentation Foundation what is it, what can you do with it and how do you get started
Windows Presentation Foundation Deliver Innovative User Interfaces Increase Developer-Designer Productivity; Leveraging Existing Code Base and Skills Set “Application = Code + Markup” A productive, unified approach to UI, media and documents to deliver unmatched UX
Why WPF? 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. We can’t think about Animation …..! 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) changes all this by introducing a new 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. you can use transformation objects to rotate, stretch, scale, and skew anything in your user interface WPF is a powerful new infrastructure based on DirectX
Overall, WPF combines the best of the old world of Windows development with new innovations for building modern, graphically rich user interfaces. Although Windows Forms applications will continue to live on for years, developers embarking on new Windows development projects should consider WPF.
What is there in WPF? In WPF, the underlying graphics technology isn’t GDI/GDI+. Instead, it’s DirectX. Remarkably, WPF applications use DirectX no matter what type of user interface you create. That means that whether you’re designing complex three- dimensional graphics or just drawing buttons and plain text, all the drawing work travels through the DirectX pipeline. One component that’s still in the picture (to a limited extent) is User32. Because WPF still relies on User32 for certain services, such as handling and routing input and sorting out which application owns which portion of screen. However, all the drawing is funnelled through DirectX.
WPF TIERS Video cards differ significantly. When WPF assesses a video card, it considers a number of factors, including the amount of RAM on the video card, support for pixel shaders and support for vertex shaders. Based on these details, it assigns a rendering tier value. WPF recognizes three rendering tiers. They are as follows: Rendering Tier 0. The video card will not provide any hardware acceleration. This corresponds to a DirectX version level of less than 7.0. Rendering Tier 1. The video card can provide partial hardware acceleration. This corresponds to a DirectX version level greater than 7.0 but less than 9.0. Rendering Tier 2. All features that can be hardware accelerated will be. This corresponds to a DirectX version level greater than or equal to 9.0.
WPF – Higher Level API A web-like layout model A rich drawing model. Support for audio and video media. Animation as a first-class programming concept Styles and templates Commands Declarative user interface. Page-based applications Resolution Independence
Evolution of WPF Although WPF is a relatively new technology, it already exists in two versions: WPF 3.0. The first version of WPF was released with two other new technologies: Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF). Together, these three technologies were called the.NET Framework 3.0 (even though the core bits of.NET weren’t changed). WPF 3.5. A year later, a new version of WPF was released as part of the.NET Framework 3.5. The new features in WPF are mostly minor refinements. Some of these bug fixes and performance improvements are available to.NET Framework 3.0 applications through the.NET Framework 3.0 Service Pack 1.
New Features in 3.5 Firefox support for XBAPs Data binding support for LINQ Support for placing interactive controls (such as buttons) inside a RichTextBox control Support for placing 2-D elements on 3-D surfaces An add-in model. The add-in model allows an application to host third-party components in a limited security context.
Architecture WPF uses a multilayered architecture. At the top, your application interacts with a high-level set of services that are completely written in managed C# code. The actual work of translating.NET objects into Direct3D textures and triangles happens behind the scenes, using a lower level unmanaged component called milcore.dll.
PresentationCore.dll Applications Controls Styling LayoutDataContentAction Presentation Framework MilCore.dll DirectX Driver
PresentationFramework.dll holds the top-level WPF types, including those that represent windows, panels, and other types of controls. It also implements higher-level programming abstractions such as styles. Most of the classes you’ll use directly come from this assembly. PresentationCore.dll holds base types, such as UIElement and Visual, from which all shapes and controls derive. If you don’t need the full window and control abstraction layer, you can drop down to this level and still take advantage of WPF’s rendering engine. WindowsBase.dll 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 milcore.dll is the core of the WPF rendering system and the foundation of the Media Integration Layer (MIL). Its composition engine translates visual elements into the triangle and textures that Direct3D expects.
What type of Applications can be built WPF intended to replace both webforms and windows forms – apps can be built to run in a browser or a standalone window Windows forms and WPF pages/windows can co- exist in the same application One WPF page/window can have both types of controls on it A new type of web application called XAML Browser Application XBAP (formerly Web Browser Application WBA)
XAML short for Extensible Application Markup Language, and pronounced “zammel” is a markup language used to instantiate.NET objects If you’re a graphic designer, that tool is likely to be a graphical design and drawing program such as Microsoft Expression Blend. Ability to integrate the workflow between developers and designers is one of the key reasons that Microsoft created XAML. When designing a WPF application in Visual Studio, the window you’re designing isn’t translated into code. Instead, it’s serialized into a set of XAML tags. When you run the application, these tags are used to generate the objects that compose the user interface.
Every element in a XAML document maps to an instance of a.NET class. The name of the element matches the name of the class exactly. For example, the element instructs WPF to create a Button object. As with any XML document, you can nest one element inside another. As you’ll see, XAML gives every class the flexibility to decide how it handles this situation. You can set the properties of each class through attributes. However, in some situations an attribute isn’t powerful enough to handle the job. In these cases, you’ll use nested tags with a special syntax.
XAML Namespaces The XAML parser also needs to know the.NET namespace where this class is located. For example, the Window class could exist in several places—it might refer to the System.Windows.Window class, or it could refer to a Window class in a third-party component, or one you’ve defined in your application. xmlns= entationhttp://schemas.microsoft.com/winfx/2006/xaml/pres entation xmlns:x="
XAML = Extensive Application Markup Language Button b1 = new Button(); b1.Content = "OK"; b1.Background = new SolidColorBrush(Colors.LightBlue); b1.Width = 100; C# Dim b1 As New Button b1.Content = "OK" b1.Background = New _ SolidColorBrush(Colors.LightBlue) b1.Width = 100 VB.NET OK LightBlue XAML Easily tool able, declarative markup Code and content are separate Can be rendered in the browser / standalone application
XAML: Unified Programming Model Programming and markup do not currently coexist very well together –Code is great for logic, but can be clunky for the job of laying out text and UIs –Markup is great for defining text and images, but is clunky when dealing with logic and user interaction WPF meshes the two technologies Unified approach to combining markup and code –Most apps written to WPF will be a mix of XAML and code
Markup language for declaratively designing and creating application UIs XAML maps XML markup to objects in the.NET Framework –Every tag maps to a class and every attribute to a property –But really independent of WPF Markup and procedural code are peers in functionality and performance –code and markup are both first class citizens –consistent model between UI, documents, and media
Coding in XAML XAML Okay C# Button b1 = new Button(); b1.Content = "Okay"; b1.Background = new SolidColorBrush(Colors.LightBlue); b1.Width = 100; XAML allows one root tag, generally an output surface
Coding with WPF and XAML Integrating markup and code XAML File Click Me! C# code-behind file using… namespace Button { public partial class Default : Panel { // Event handler void Button_Click (object sender, System.Windows.Controls.ClickEventArgs e) { btn1.Background = System.Windows.Media.Brushes.Red; } } Integrating markup and code XAML File Click Me! C# code-behind file using… namespace Button { public partial class Default : Panel { // Event handler void Button_Click (object sender, System.Windows.Controls.ClickEventArgs e) { btn1.Background = System.Windows.Media.Brushes.Red; } }
Application Object Application object acts as container for more complex applications MainWindow Application events like Startup & Shutdown
Content Model WPF offers strong separation of behaviour (API) and presentation Behaviour (API) consists of Commands, Properties, Events & Methods Presentation of controls is controlled by Nested content Templates
WPF Content Models: In WPF, different classes of elements are categorized based on the type and number of their logical children. These logical children are also referred as the "content" of the control. WPF defines several different "content models" for various different classes of elements. The controls that can contain a single item (single logical child of type Object) are called "content controls". These controls derive from the Content Control base class. Controls that can contain a collection of items (many logical children of type Object) are called "items controls". These controls derive from the Items Control base class. There are different kinds of content models available in WPF like Controls content model, Decoder content model, Panel content model, TextBox and TextBlock content model.