So it is 2020 and presumably you are a newcomer to C# confused by the myriad of GUI options available to you in the quickly changing .NET ecosystem. Before diving into WPF in future articles, this article will examine the pros and cons of the most viable GUI frameworks and where they work so you can determine if WPF is viable for your project. Finally, I will address the shakeup that Microsoft introduced in Build 2020.
I will separate GUI frameworks into two categories: Codegen-based and XAML-based. Web-based approaches such as ASP.NET Core with JS-based frontends (Vue.js, React, Angular) and Blazor will not be discussed here.
Code Generation Frameworks
Windows Forms
Most newcomers and many college-level C# GUI courses choose this framework when getting started and that is a mistake. While WinForms has the best drag-and-drop GUI editor of all major .NET GUI frameworks, it also has the worst out-of-the-box controls and extensibility. Introduced in 2002, WinForms is an extensive wrapper around Win32API. As such, customization beyond the built-in control features often requires specific Win32API knowledge to change behavior or customize appearance, dealing with flickering issues, or even redrawing the entire control from scratch. Then you may also need to write additional code to support your customization in the WinForms Designer if you want correct visualization in the Designer.
Compatibility
- .NET Framework and .NET Core 3+
- Windows Desktop
- Cross-platform with many caveats via Mono
Pros
- Easiest start because of the WinForms Designer
Cons
- Very difficult to customize
- Data binding is limited and awkward
- Encourages code coupling leading to poor testability and poor maintainability
- Creating dynamic collections of controls in WinForms often requires code and defeats the point of the Designer
- Poor transparency support and tough to troubleshoot redraw flickering
Verdict
There is only one set of conditions where using WinForms is viable: 1. You don’t care about UI appearance or behavior beyond the limited WinForms controls, 2. You don’t care about maintainability, and 3. You do not have time to go through the learning curve of a better UI framework. I like to say that WinForms makes the first 20% of an application very easy and the last 80% very hard. As your app’s complexity grows, the architecture around WinForms is not up to the task. There are some third-party commercial control libraries to help ease the pain, but they’re not for everybody.
XAML Frameworks
These frameworks use an XML-based language called Extensible Application Markup Languages (XAML) to define the view. User interfaces are inherently hierarchical with many parent-child relationships so XAML is a good fit to define the UI as opposed to code generation which is much more flat. Furthermore, XAML allows easy runtime generation of collections of controls through data templates and data binding. Most XAML frameworks have support for Hot Reload which allows you to launch the application via your IDE, edit XAML, and see updates live as your app is running. These features and more have made XAML the mainstay of most .NET GUI offerings.
Many developers pair XAML Frameworks with the MVVM (Model-View-ViewModel) architecture. This architecture allows the ViewModel, a C# class that contains data to display and executable commands, to be separated from the XAML-based View which contains the UI control hierarchy and styling. With care, these ViewModels have potential portability to other XAML-based frameworks and are unit testable. One common practice is to target desktop with a WPF or UWP project and target mobile with a Xamarin project while using the same ViewModel.
Windows Presentation Foundation
WPF was released in late 2006 and Microsoft learned from many flaws within WinForms. First, WPF detaches from Win32API and is built from the ground up using DirectX. Second, WPF introduced XAML which is a markup language performing a similar role as HTML in isolating view code. Last, good data binding was introduced to eliminate a significant portion of glue code and data templates solved the problem of writing code to dynamically generate collections of controls.
Compatibility
- .NET Framework and .NET Core 3+
- Windows Desktop only
Pros
- Mature ecosystem with many articles and StackOverflow posts
- Excellent community libraries
- Flexible customization
Cons
- Restricted to Windows Desktop
- WPF Designer is a trap and not worth using
- Steep learning curve with pitfalls
Verdict
WPF is a mature, flexible GUI framework that is still good for developing Windows Desktop apps today. There is no uncertainty about the future since WPF is deep into maintenance mode with only bug fixes and minor updates. The default styling may look a bit dated, but excellent styling libraries created by the community have helped modernize its look.
Universal Windows Platform
UWP for Windows 10 is Microsoft’s successor to WPF. Initially stumbling with its identity and Microsoft’s failed push at Windows 10 Phone, UWP did not capture the Windows developer scene despite its improved offerings in features. Like WPF, it also uses XAML for designing views and has GPU hardware acceleration.
Compatibility
- .NET Framework and .NET Core 3+
- Windows 10: Desktop, Phone, XBox One, HoloLens, IoT
Pros
- Improves upon WPF features with Compiled Bindings, XAML Islands, Input Gestures, new controls, and more
- Modern styling out-of-the-box
- Simplest path to Microsoft Store
- Multiple language support: C#, C++, VB, and JS
Cons
- Unclear future
- Limited to Windows 10
Verdict
UWP is, for the time being, the premiere Microsoft offering for Windows Desktop GUI.
Avalonia
Avalonia is a recent community-developed GUI framework. Its main focus is providing cross-platform support.
Compatibility
- .NET Framework 4.61+ and .NET Core 2+
- Windows, Linux, and OSX Desktop
Pros
- Cross-Platform
- Reasonable docs for third-party open source
Cons
- Mobile support still experimental
- Relative lack of detailed articles and StackOverflow posts compared to WPF and UWP
- Less options for third-party styling and controls
Verdict
Strongly consider Avalonia if your main concern is a cross-platform Desktop GUI.
Xamarin.Forms
Xamarin.Forms (and Xamarin in general) is Microsoft’s cross-platform offering to primarily target mobile though desktop options are available.
Compatibility
- iOS 9+
- Android 4.4 (API 19)+ or Android 5.0 (API 21)+ recommended
- Windows 10 UWP
- GTK#, macOS, and WPF targets under development with mixed levels of support
Pros
- Transforms UI elements into native controls with native styling
- Easy to pull in Xamarin.Essentials for mobile system features
- Allows platform-specific behavior for Android and iOS
Cons
- Requires VS for Mac to target iOS and macOS
Verdict
Xamarin.Forms is a good approach when mobile targeting is your primary need, but some Desktop targets are also available. In late 2021, Xamarin.Forms is planned to be transition into (or rebranded as) a part of MAUI.
Uno Platform
The Uno Platform is another relative newcomer developing a cross-platform solution for the .NET GUI ecosystem. Uno achieves non-Windows support by relying upon Xamarin and the Mono-Wasm runtime and supports Windows 10 through UWP/WinUI. I will not discuss Pros and Cons as I’m not familiar enough with Uno.
Compatibility
- Windows 10 (UWP/WinUI)
- iOS
- macOS
- Android
- WebAssembly
Verdict
This is an up-and-coming framework to keep your eye on and experiment with. Has potential to be the broadest targeting GUI framework until MAUI is released.
Abandoned Technologies
Silverlight and WebForms are no longer being maintained and should never be considered for use.
Build 2020
During Build 2020, Microsoft announced three major GUI-related projects: WinUI 3, MAUI, and Project Reunion. WinUI 3 is the next version of native Windows UI controls and isolates controls from UWP to make them available across non-UWP frameworks. MAUI (.NET Multi-platform App UI) will be the successor of Xamarin.Forms, but much more flexible in targeting other platforms in a true cross-platform attempt to compete with non-.NET techs like Flutter and Electron. Project Reunion is Microsoft’s unification of framework-specific APIs, especially from UWP. Microsoft’s plan to decouple these features from OS versions and make them available via NuGet will solve some OS versioning headaches. Older technologies (WinForms, WPF) and non-.NET programming languages will also be able to take advantage of the newest features.