Windows Presentation Foundation

Last updated
Windows Presentation Foundation (WPF)
Original author(s) Microsoft
Developer(s) .NET Foundation
Initial releaseNovember 21, 2006;17 years ago (2006-11-21)
Stable release
v8.0.0 / November 14, 2023;6 months ago (2023-11-14) [1]
Repository
Written in C#, C++, C
Operating system Microsoft Windows
Platform .NET Framework, .NET
Type Software framework
License MIT License
Website learn.microsoft.com/en-us/dotnet/desktop/wpf/

Windows Presentation Foundation (WPF) is a free and open-source user interface framework for Windows-based desktop applications. WPF applications are based in .NET, and are primarily developed using C# and XAML. [2]

Contents

Originally developed by Microsoft, WPF was initially released as part of .NET Framework 3.0 in 2006. In 2018, Microsoft released WPF as open source under the MIT License.

Overview

WPF employs XAML, an XML-based language, to define and link various interface elements, and uses C# to define program behavior. [3] WPF applications are deployed as standalone desktop programs.

WPF aims to unify a number of common user interface elements, such as 2D/3D rendering, fixed and adaptive documents, typography, vector graphics, runtime animation, and pre-rendered media. These elements can then be linked and manipulated based on various events, user interactions, and data bindings. [4]

WPF runtime libraries are included with all versions of Microsoft Windows since Windows Vista and Windows Server 2008.

At the Microsoft Connect event on December 4, 2018, Microsoft announced releasing WPF as open source project on GitHub. It is released under the MIT License. Windows Presentation Foundation has become available for projects targeting the .NET software framework, however, the system is not cross-platform and is still available only on Windows. [5] [6]

Code examples

Screenshot of developing a basic Windows Presentation Foundation (WPF) UI application in Visual Studio 2022. XAML is used to define the layout, while C# is used to define the interactive behavior. WPF development example in Visual Studio 2022.png
Screenshot of developing a basic Windows Presentation Foundation (WPF) UI application in Visual Studio 2022. XAML is used to define the layout, while C# is used to define the interactive behavior.

In WPF, screens and other UI elements are defined using a pair of files: a XAML file and an associated C# file with the extension .xaml.cs, often referred to as a "code-behind". The XAML file declaratively defines the layout, contents and other properties of the UI element, while the C# file allows exposure of code entry points for interactivity. [3]

A basic example of an interactive Hello, World! program could be created like so:

MainWindow.xaml:

<Windowx:Class="WpfExample.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow"Height="200"Width="300"><StackPanelOrientation="Vertical"><TextBlockText="What is your name?"/><TextBoxx:Name="NameInputTextBox"/><Buttonx:Name="SubmitButton"Click="SubmitButton_Click"><TextBlockText="Submit"/></Button><TextBlockx:Name="ResultTextBlock"/></StackPanel></Window>

MainWindow.xaml.cs:

usingSystem.Windows;namespaceWpfExample{publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();}privatevoidSubmitButton_Click(objectsender,RoutedEventArgse){varname=this.NameInputTextBox.Text;this.ResultTextBlock.Text=$"Hello {name}!";}}}

In the above example, a UI element called MainWindow is declared as a subclass of the built-in Window class. The XAML file defines the layout, which in this example is a vertical collection of controls - a textblock outlining instructions to the user, a textbox for the user to type their name, a button to submit, and a results text box. When the button is clicked, the method SubmitButton_Click is called, which is defined in the .xaml.cs file. This function updates the final textblock to contain a message greeting the user, addressing them by their submitted name.

Features

Direct3D

Graphics, including desktop items like windows, are rendered using Direct3D. This allows the display of more complex graphics and custom themes, at the cost of GDI's wider range of support and uniform control theming. It allows Windows to offload some graphics tasks to the GPU. This reduces the workload on the computer's CPU. GPUs are optimized for parallel pixel computations. This tends to speed up screen refreshes at the cost of decreased compatibility in markets where GPUs are not necessarily as powerful, such as the netbook market.

WPF's emphasis on vector graphics allows most controls and elements to be scaled without loss in quality or pixelization, thus increasing accessibility. With the exception of Silverlight, Direct3D integration allows for streamlined 3D rendering. In addition, interactive 2D content can be overlaid on 3D surfaces natively. [7] [8]

Data binding

Media services

Templates

Control templates

Data templates

Animations

Imaging

Effects

Documents

Text

Interoperability

To enable the use of WinForms, the developer executes this from their WPF C# code:

 System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop();

Alternative input

Accessibility

XAML

Following the success of markup languages for web development, WPF introduces eXtensible Application Markup Language (XAML; /ˈzæməl/ ), which is based on XML. XAML is designed as a more efficient method of developing application user interfaces. [17] The specific advantage that XAML brings to WPF is that XAML is a completely declarative language, allowing the developer (or designer) to describe the behavior and integration of components without the use of procedural programming. Although it is rare that an entire application will be built completely in XAML, the introduction of XAML allows application designers to more effectively contribute to the application development cycle. Using XAML to develop user interfaces also allows for separation of model and view, which is considered a good architectural principle. In XAML, elements and attributes map to classes and properties in the underlying APIs.

As in web development, both layouts and specific themes are well suited to markup, but XAML is not required for either. Indeed, all elements of WPF may be coded in a .NET language (C#, VB.NET). The XAML code can ultimately be compiled into a managed assembly in the same way all .NET languages are.

Architecture

The WPF architecture. Blue elements are Windows components; brown ones are WPF components. WPF.svg
The WPF architecture. Blue elements are Windows components; brown ones are WPF components.

The architecture of WPF spans both managed code and native code components. However, the public API exposed is only available via managed code. While the majority of WPF is in managed code, the composition engine which renders the WPF applications is a native component. It is named Media Integration Layer (MIL) and resides 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. [18] [19] The Desktop Window Manager also uses the MIL for desktop and window composition. [20] The media codecs are also implemented in unmanaged code, and are shipped as windowscodecs.dll. [18] In the managed world, PresentationCore (presentationcore.dll) provides a managed wrapper for MIL and implements the core services for WPF, [18] 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. [19] PresentationFramework (presentationframework.dll) implements the end-user presentational features, including layouts, time-dependent, story-board based animations, and data binding. [19]

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. In the initial release, however, the set of expressions supported is closed. [19] 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. [19] 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. [21]

The interface elements of a WPF application are maintained as a class of Visual objects. 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 and transformation instructions, along with other visual attributes. Thus the entire application is represented as a collection of composition nodes, which are stored in a buffer in the system memory. 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. [19] 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. On the user visible side, animations are specified 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. [22] All WPF applications start with two threads: one for managing the UI and another background thread for handling rendering and repainting. [23] Rendering and repainting is managed by WPF itself, without any developer intervention. The UI thread houses 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. Microsoft recommends that the event handlers only update the properties to reflect new content for application responsiveness, with the new content being generated or retrieved in a background thread. [23] The render thread picks up a copy of the visual tree 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 just the changed pixels. WPF supports an extensible layout model. Layout is divided into two phases: Measure; and Arrange. The Measure phase recursively calls all elements and determines the size they will take. In the Arrange phase, the child elements are recursively arranged by their parents, invoking the layout algorithm of the layout module in use. [19] [24]

Deployment

WPF applications are Windows-only standalone desktop executables.

Historically, WPF supported compiling to XBAP, a file format intended to be shown in web browsers via a NPAPI plugin, but NPAPI and XBAP support was phased out of support by browsers, and XBAP compilation is now no longer included for WPF for .NET. [25] [26]

Development Tools

A number of development tools are available for developing Windows Presentation Foundation applications.

WPF is related to a host of other UI frameworks, including:

Related Research Articles

ClearType is Microsoft's implementation of subpixel rendering technology in rendering text in a font system. ClearType attempts to improve the appearance of text on certain types of computer display screens by sacrificing color fidelity for additional intensity variation. This trade-off is asserted to work well on LCD flat panel monitors.

<span class="mw-page-title-main">Graphics Device Interface</span> Microsoft Windows API

The Graphics Device Interface (GDI) is a legacy component of Microsoft Windows responsible for representing graphical objects and transmitting them to output devices such as monitors and printers. It was superseded by DirectDraw API and later Direct2D API. Windows apps use Windows API to interact with GDI, for such tasks as drawing lines and curves, rendering fonts, and handling palettes. The Windows USER subsystem uses GDI to render such UI elements as window frames and menus. Other systems have components that are similar to GDI; for example: Mac OS has QuickDraw, and Linux and Unix have X Window System core protocol.

OpenType is a format for scalable computer fonts. Derived from TrueType, it retains TrueType's basic structure but adds many intricate data structures for describing typographic behavior. OpenType is a registered trademark of Microsoft Corporation.

<span class="mw-page-title-main">Dots per inch</span> Measure of dot density

Dots per inch is a measure of spatial printing, video or image scanner dot density, in particular the number of individual dots that can be placed in a line within the span of 1 inch (2.54 cm). Similarly, dots per centimetre refers to the number of individual dots that can be placed within a line of 1 centimetre (0.394 in).

Extensible Application Markup Language is a declarative XML-based language developed by Microsoft for initializing structured values and objects. It is available under Microsoft's Open Specification Promise.

<span class="mw-page-title-main">Windows Forms</span> Graphical user interface software library

Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .NET, .NET Framework or Mono, providing a platform to write client applications for desktop, laptop, and tablet PCs. While it is seen as a replacement for the earlier and more complex C++ based Microsoft Foundation Class Library, it does not offer a comparable paradigm and only acts as a platform for the user interface tier in a multi-tier solution.

A computer font is implemented as a digital data file containing a set of graphically related glyphs. A computer font is designed and created using a font editor. A computer font specifically designed for the computer screen, and not for printing, is a screen font.

<span class="mw-page-title-main">Font rasterization</span> Process of converting text from vector to raster

Font rasterization is the process of converting text from a vector description to a raster or bitmap description. This often involves some anti-aliasing on screen text to make it smoother and easier to read. It may also involve hinting—information embedded in the font data that optimizes rendering details for particular character sizes.

Desktop Window Manager is the compositing window manager in Microsoft Windows since Windows Vista that enables the use of hardware acceleration to render the graphical user interface of Windows.

Compared with previous versions of Microsoft Windows, features new to Windows Vista are very numerous, covering most aspects of the operating system, including additional management features, new aspects of security and safety, new I/O technologies, new networking features, and new technical features. Windows Vista also removed some others.

Resolution independence is where elements on a computer screen are rendered at sizes independent from the pixel grid, resulting in a graphical user interface that is displayed at a consistent physical size, regardless of the resolution of the screen.

Windows Vista has many significant new features compared with previous Microsoft Windows versions, covering most aspects of the operating system.

<span class="mw-page-title-main">Microsoft Silverlight</span> Application framework for writing and running rich Internet applications

Microsoft Silverlight is a discontinued application framework designed for writing and running rich internet applications, similar to Adobe's runtime, Adobe Flash. While early versions of Silverlight focused on streaming media, later versions supported multimedia, graphics, and animation, and gave support to developers for CLI languages and development tools. Silverlight was one of the two application development platforms for Windows Phone, but web pages using Silverlight did not run on the Windows Phone or Windows Mobile versions of Internet Explorer, as there was no Silverlight plugin for Internet Explorer on those platforms.

XAML Browser Applications are Windows Presentation Foundation (.xbap) applications that were intended to run inside a web browser such as Firefox or Internet Explorer through the NPAPI interface. Due to NPAPI being phased out in recent years, and from lack of support, there are currently no browsers that support XBAP applications.

Direct2D is a 2D vector graphics application programming interface (API) designed by Microsoft and implemented in Windows 10, Windows 8, Windows 7 and Windows Server 2008 R2, and also Windows Vista and Windows Server 2008.

Model–view–viewmodel (MVVM) is an architectural pattern in computer software that facilitates the separation of the development of a graphical user interface —be it via a markup language or GUI code—from the development of the business logic or back-end logic such that the view is not dependent upon any specific model platform.

DirectWrite is a text layout and glyph rendering API by Microsoft. It was designed to replace GDI/GDI+ and Uniscribe for screen-oriented rendering and was first shipped with Windows 7 and Windows Server 2008 R2, as well as Windows Vista and Windows Server 2008. DirectWrite is hardware-accelerated when running on top of Direct2D, but can also use the CPU to render on any target, including a GDI bitmap.

Microsoft Silverlight is an application framework for writing and running rich web applications that was actively developed and marketed by Microsoft from 2007 to 2012. This is a technical overview of the platform's history.

Windows UI Library is a user interface API that is part of the Windows Runtime programming model that forms the backbone of Universal Windows Platform apps for the Windows 8, Windows 8.1, Windows 10 and Windows Phone 8.1 operating systems. It enables declaring user interfaces using Extensible Application Markup Language (XAML) technology.

References

  1. "v8.0.0". github.com. 2023-11-14. Retrieved 2023-11-21.
  2. "What is Windows Presentation Foundation - WPF .NET". learn.microsoft.com. 2023-06-02. Retrieved 2024-05-15.
  3. 1 2 dotnet-bot. "XAML Overview (WPF)". msdn.microsoft.com. Retrieved 31 March 2018.
  4. Sells, Chris; Griffiths, Ian (2007). Programming WPF: Building Windows UI with Windows Presentation Foundation. "O'Reilly Media, Inc.". ISBN   9780596554798.
  5. Martin, Jeff (4 December 2018). "Microsoft Open Sources WPF, WinForms, and WinUI". InfoQ. Retrieved 2018-12-06.
  6. Hanselman, Scott (4 December 2018). "Announcing WPF, WinForms, and WinUI are going Open Source" . Retrieved 2018-12-06.
  7. "Introducing Windows Presentation Foundation". msdn.microsoft.com. Retrieved 31 March 2018.
  8. 1 2 "What's New in WPF 3.5? Here's Fifteen Cool Features..." Retrieved 2007-10-14.
  9. Graphics and Multimedia. Msdn.Microsoft.com. Retrieved on 2013-08-29.
  10. 1 2 3 4 "Animation overview". MSDN . Retrieved 2007-10-14.
  11. "Typography in Windows Presentation Foundation". msdn.microsoft.com. Retrieved 31 March 2018.
  12. dotnet-bot. "ClearType Overview". msdn.microsoft.com. Retrieved 31 March 2018.
  13. "Disable Antialiasing". social.msdn.microsoft.com. Retrieved 31 March 2018.
  14. "My first thoughts on WPF with VS 2008 RTM and a few questions". social.msdn.microsoft.com. Retrieved 31 March 2018.
  15. WPF 4 (VS 2010 and .NET 4.0 Series) - ScottGu's Blog. Weblogs.asp.net. Retrieved on 2013-08-29.
  16. Xansky. "UI Automation Overview". msdn.microsoft.com. Retrieved 31 March 2018.
  17. MacDonald, Matthew (2010). Pro WPF in VB 2010: Windows Presentation Foundation in .NET 4. Apress. ISBN   9781430272403.
  18. 1 2 3 Chris Anderson. "MIL information" . Retrieved 2007-10-14.
  19. 1 2 3 4 5 6 7 "WPF Architecture". MSDN . Retrieved 2007-10-14.
  20. Schechter, Greg (June 9, 2006). "How underlying WPF concepts and technology are being used in the DWM". MSDN Blogs. Microsoft . Retrieved 2015-07-27.
  21. "Create a WPF application in Visual Studio". docs.microsoft.com.
  22. "WPF Architecture". docs.microsoft.com.
  23. 1 2 "Threading Model". MSDN . Retrieved 2007-10-14.
  24. "The Layout System". MSDN . Retrieved 2007-10-14.
  25. adegeo. "FAQ about XBAP supportability". learn.microsoft.com. Retrieved 2024-05-15.
  26. kexugit (2011-03-09). "IE9 - XBAPs Disabled in the Internet Zone". learn.microsoft.com. Retrieved 2024-05-15.
  27. Retrieved from http://www.microsoft.com/en-au/download/details.aspx?id=23072.
  28. "Avalonia UI - Cross-Platform UI Framework for .NET". Avalonia UI. Retrieved 2024-05-16.
  29. "The Official Microsoft ASP.NET Site". The Official Microsoft ASP.NET Site. Retrieved 31 March 2018.

Bibliography