Page 28 - MSDN Magazine, May 2019
P. 28
XAML
Custom
XAML Controls
Jerry Nixon
As an enterprise developer, you know your way around SQL Server. You know .NET Web services. And, for you, designing beautiful XAML interfaces (probably with Windows Presentation Foundation [WPF]) is child’s play. Like thousands of other career developers, Microsoft technologies pad your resumé and you clip MSDN Magazine articles like this one and pin them to your Kanban board. Get your scissors, this article is table stakes.
It’s time to boost your expertise with XAML controls. The XAML framework offers an extensive library of controls for UI develop- ment, but to accomplish what you want, you need more. In this article I’ll show you how to get the results you want using XAML custom controls.
Custom Controls
There are two approaches to creating custom controls in XAML: user controls and templated controls. User controls are an easy, designer- friendly approach to creating a reusable layout. Templated controls offer a flexible layout with a customizable API for developers. As is the case in any language, sophisticated layouts can produce thou- sands of lines of XAML that can be difficult to navigate productively. Custom controls are an effective strategy for reducing layout code.
Choosing the correct approach will impact how successfully you can use and reuse the controls in your application. Here are some considerations to help you get started.
Simplicity. Easy is not always simple, but simple is always easy. User controls are simple and easy. Developers on any level can deliver them with little reach for documentation.
Design experience. Many developers love the XAML designer. Templated control layouts can be built in the designer, but it’s the user control that embraces the design-time experience.
API surface. Building an intuitive API surface lets developers easily consume it. User controls support custom properties, events and methods, but templated controls are the most flexible.
Flexible visuals. Providing a great default experience lets devel- opers consume controls with ease. But flexible controls support re-templated visuals. Only templated controls support re-templating.
To sum, user controls are optimal for simplicity and design experience, while templated controls give you the best API surface and the most flexible visuals.
This article discusses:
• Logic encapsulation
• Component reusability
• Control templating
• Data binding
Technologies discussed:
Windows Presentation Foundation, Universal Windows Platform, XAML, C#
Code download available at:
github.com/JerryNixon/MsdnMgazineCustomControls
22 msdn magazine