Original author(s) | Microsoft |
---|---|
Developer(s) | .NET Foundation |
Initial release | November 21, 2006 |
Stable release | v8.0.5 / May 14, 2024 [1] |
Repository | |
Written in | C#, C++, C |
Operating system | Microsoft Windows |
Platform | .NET Framework, .NET |
Type | Software framework |
License | MIT License |
Website | learn |
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]
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. [3] WPF's design and its layout language XAML have been adopted by multiple other UI frameworks, such as UWP, .NET MAUI, and Avalonia.
WPF employs XAML, an XML-based language, to define and link various interface elements, and uses C# to define program behavior. [4] WPF applications are deployed as standalone desktop programs.
WPF supports 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. [5]
WPF runtime libraries are included with all versions of Microsoft Windows since Windows Vista and Windows Server 2008. [6] [7]
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. [3] [8]
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. [4]
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 block. 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.
WPF supports most modern media types, including vector and raster images, audio, video, and can support both 2D and 3D rendering. [9] It also supports implementing graphical effects on visual elements, such as HLSL pixel shaders and built-in effects such as blurs and drop shadows. [10] [11]
WPF mainly relies on vector graphics, which allows most controls and elements to be scaled without loss in quality or pixelization. [12] [13]
WPF employs data binding, a technique of propagating changes between user interface elements and the object model of the program. [14] For instance, a textblock defined in XAML could bind its contents to a string property stored on a C# object in the following way:
<TextBlockText="{Binding Username}"/>
publicclassExampleViewModel{publicstringUsername{get;set;}}
This would make the textblock display the value of the Username
property.
The direction data binding happens is configurable - it can be defined to go UI-to-source (eg. text entered into a textbox being propagated to a bound string property in code), or source-to-UI (eg. a visual clock being updated to display the current time stored in code), or bi-directional. [14]
Data binding doesn't have any restrictions on types, and it is possible to bind structs, classes, or collections. Converters can be used to transpose the values and types being used during binding - for example, binding a textbox to a DateTime property, but using a converter to display the time as a formatted localised date string. [14]
Most properties on built-in controls can be databound, and custom-made controls can create bindable properties by defining Dependency Properties. [14] [15]
MVVM, the architectural pattern encouraged by Microsoft for WPF developers, relies heavily on data binding. [16]
Using templates and styles, developers can define the visuals and structure of UI elements.
A style is a combination of property settings that can be applied to a UI element with a single property attribute. For example, a "blue radio button" style could be created and then be reused on any number of radio button controls throughout the program. Styles can alter collections of properties of controls, but are not not intended for significant structural changes.
Templates are a mechanism for defining alternative UI for portions of a WPF application. There are several template types available in WPF for different scenarios, but they all have the general purpose of defining the contents, layout and structure of a UI element. [17]
In WPF, many visual properties can be animated. This is exposed through dependency properties, which is the same underlying system that data binding relies on. WPF animations are time-based, as opposed to frame-based. [18]
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. [18]
Animations can be grouped into Storyboards, which are the primary way to start, stop, pause and otherwise manipulate animations. [18]
WPF natively supports paginated documents. It provides the DocumentViewer
class, which is for reading fixed layout documents. The FlowDocumentReader
class offers different view modes such as per-page or scrollable and also reflows text if the viewing area is resized. It supports both the XML Paper Specification and Open Packaging Conventions. [19]
WPF includes a number of text rendering features, including OpenType, TrueType, and OpenType CFF (Compact Font Format) fonts. This means that WPF can support a wide number of text features, including ligatures, old-style numerals, swash variants, fraction, superscript and subscript, small caps, ruby characters, glyph substitution, multiple baselines, and kerning. [20]
WPF handles texts in Unicode, and handles texts independent of global settings, such as system locale. In addition, fallback mechanisms are provided to allow writing direction (horizontal versus vertical) handled independent of font name; building international fonts from composite fonts, using a group of single-language fonts; composite fonts embedding. Font linking and font fallback information is stored in a portable XML file, using composite font technology. [21] The XML file has extension .CompositeFont.
The WPF text engine also supports built-in spell checking. It also supports such features as automatic line spacing, enhanced international text, language-guided line breaking, hyphenation, and justification, bitmap effects, transforms, and text effects such as shadows, blur, glow, rotation etc. Animated text is also supported; this refers to animated glyphs, as well as real-time changes in position, size, color, and opacity of the text.
WPF text rendering takes advantage of advances in ClearType technology, such as sub-pixel positioning, natural advance widths, Y-direction anti-aliasing, hardware-accelerated text rendering, as well as aggressive caching of pre-rendered text in video memory. [22] ClearType cannot be turned off in older WPF 3.x applications. [23]
Windows Forms features are possible through the use of the ElementHost
and WindowsFormsHost
classes.
To enable the use of WinForms, the developer executes this from their WPF C# code: [24]
System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop();
WPF programs, via the P/Invoke feature of the CLR, can access native functionality such calling functions from Windows libraries. This allows the ability to communicate and operate other parts of the operating system, including unmanaged libraries. [25]
WPF supports Windows Ink for pen-based input, [26] and multi-touch input on Windows 7 and above. [27] It also supports Microsoft UI Automation to allow developers to create accessible interfaces, and to expose the UI to automated test frameworks. [28]
Following the success of markup languages for web development, WPF introduced 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. [29] XAML is a declarative language, meaning the developer (or designer) describes 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. [30] 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.
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. [31] [32]
WPF is used to develop Visual Studio, Microsoft's flagship IDE, and was used for developing Microsoft Expression Blend. [33]
WPF and its layout language, XAML, have influenced multiple other UI frameworks.
Silverlight (codenamed WPF/E - WPF Everywhere), released in 2007, is a deprecated cross-browser browser plugin which contained WPF-based technology, including XAML, that provided features including video, vector graphics, and animations. Specifically, it was provided as an add-on for Mozilla Firefox, Internet Explorer 6 and above, Google Chrome 42 and below and Apple Safari. Microsoft encouraged developers to stop using Silverlight in 2015, [34] and support was officially ended in 2021. [35]
After becoming open source in 2018, WPF was forked for the project Avalonia, an open-source .NET cross-platform XAML-based UI framework, distributed under the MIT License. While WPF only is intended for Windows, Avalonia also supports builds for web (via WebAssembly), MacOS, Android, iOS, and Linux. Avalonia's name references WPF's in-development codename ("Avalon"), and markets itself as "a spiritual successor to WPF". [36] Avalonia is currently used in tools made by Unity, GitHub and JetBrains. [37]
OpenSilver is another framework designed to facilitate the migration of WPF solutions to the web. Leveraging WebAssembly enables WPF applications to run in modern web browsers without requiring plugins. Distributed under the MIT License, OpenSilver prioritizes compatibility with WPF and continues to expand its feature set to improve WPF coverage. OpenSilver 3.1 [38] , released on December 10, 2024, added several key improvements to WPF coverage, thus reinforcing commitments toward WPF compatibility as part of its core objectives on the development roadmap.
XAML, which was first designed for WPF, has been adopted for other similar Microsoft-developed UI libraries, such as UWP, [39] designed for Windows 10, Windows 11, Xbox One and Xbox Series S/X applications, and .NET MAUI (formerly Xamarin.Forms), designed for creating cross-platform native Android and iOS apps. [40]
Developers of WPF applications typically use Microsoft Visual Studio. Visual Studio contains a combination XAML editor and WPF visual designer, beginning with Visual Studio 2008. [41] Prior to Visual Studio 2008, the WPF designer add-in, codenamed Cider, was the original release of a WYSIWYG editor for creating WPF windows, pages, and user controls. It was available for Visual Studio 2005 as a Visual Studio 2005 extensions for .NET Framework 3.0 CTP for the initial release of WPF. [42]
Visual Studio is not strictly required develop WPF projects, as solutions can be built in the command line using MSBuild. [43]
Microsoft Blend is a designer-oriented tool that provides an artboard for the creation of WPF applications with 2D and 3D graphics, text and forms content. It generates XAML that may be exported into other tools and shares solution (sln files) and project formats (csproj, vbproj) with Microsoft Visual Studio. Microsoft Expression Design is a bitmap and 2D-vector graphics tool for exporting to XAML.
Visual Basic (VB), originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visual Basic language, the last version of which was Visual Basic 6.0. Although the ".NET" portion of the name was dropped in 2005, this article uses "Visual Basic [.NET]" to refer to all Visual Basic languages released since 2002, in order to distinguish between them and the classic Visual Basic. Along with C# and F#, it is one of the three main languages targeting the .NET ecosystem. Microsoft updated its VB language strategy on 6 February 2023, stating that VB is a stable language now and Microsoft will keep maintaining it.
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.
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.
Microsoft Blend for Visual Studio is a user interface design tool developed and sold by Microsoft for creating graphical interfaces for web and desktop applications that blend the features of these two types of applications. It is an interactive, WYSIWYG front-end for designing XAML-based interfaces for Windows Presentation Foundation, Silverlight and UWP applications. It was one of the applications in the Microsoft Expression Studio suite before that suite was discontinued.
Microsoft Active Accessibility (MSAA) is an application programming interface (API) for user interface accessibility. MSAA was introduced as a platform add-on to Microsoft Windows 95 in 1997. MSAA is designed to help Assistive Technology (AT) products interact with standard and custom user interface (UI) elements of an application, as well as to access, identify, and manipulate an application's UI elements. AT products work with MSAA enabled applications in order to provide better access for individuals who have physical or cognitive difficulties, impairments, or disabilities. Some examples of AT products are screen readers for users with limited sight, on screen keyboards for users with limited physical access, or narrators for users with limited hearing. MSAA can also be used for automated testing tools, and computer-based training applications.
ClickOnce is a component of Microsoft .NET Framework 2.0 and later, and supports deploying applications made with Windows Forms or Windows Presentation Foundation. It is similar to Java Web Start for the Java Platform or Zero Install for Linux.
Microsoft UI Automation (UIA) is an application programming interface (API) that allows one to access, identify, and manipulate the user interface (UI) elements of another application.
Windows Vista has many significant new features compared with previous Microsoft Windows versions, covering most aspects of the operating system.
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.
Visual Studio is an integrated development environment (IDE) developed by Microsoft. It is used to develop computer programs including websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platforms including Windows API, Windows Forms, Windows Presentation Foundation (WPF), Microsoft Store and Microsoft Silverlight. It can produce both native code and managed code.
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.
Text Template Transformation Toolkit is a free and open-source template-based text generation framework. T4 source files are usually denoted by the file extension ".tt".
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.
Microsoft started development on the .NET Framework in the late 1990s originally under the name of Next Generation Windows Services (NGWS). By late 2001 the first beta versions of .NET Framework 1.0 were released. The first version of .NET Framework was released on 13 February 2002, bringing managed code to Windows NT 4.0, 98, 2000, ME and XP.
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.
DotNetBrowser is a proprietary .NET library that provides a Chromium-based engine which can be used to load and display web pages. It is developed and supported by TeamDev since 2015.
Avalonia is a free and open-source .NET cross-platform XAML-based UI framework inspired by WPF/UWP and distributed under the MIT License. Avalonia supports the MVVM pattern.
{{cite book}}
: External link in |title=
(help)