Page 29 - MSDN Magazine, May 2019
P. 29
Figure 1 Prototyped UI
If you decide to start with a user control and migrate to a templated control, you have some work ahead of you. But, it’s not terminal. This article starts with a user control and moves to a templated control. It’s important to recognize that many reusable layouts only require user controls. It’s also reasonable to open a line-of-business solution and find both user controls and templated controls.
A New User Story
Youneedtogetnewuserstoconsenttotheend-userlicenseagree- ment (EULA) in your new application. As you and I both know, no user wants to consent to a EULA. Still, legal needs to ensure users check “I agree” before continuing. So even if the EULA is confusing, you’re going to make sure the XAML interface is clean and intuitive. You start prototyping; you add a TextBlock, a CheckBox, a Button, as shown in Figure 1, and then you start thinking.
Prototyping in the XAML designer is fast. And it’s easy because you took the time to learn the tooling. But what about other forms in your application? You might need this functionality elsewhere. Encapsula- tion is a design pattern used to hide complex logic from the consumer. Don’t repeat yourself (DRY) is another, focusing on code reuse. XAML offers both through user controls and custom controls. As a XAML developer you know custom controls are more powerful than user controls, but they’re not as simple.
mise of available functionality. My goal is to reuse them on a page, taking advantage of their support for Visual State Management, resources, styles and data binding. Their XAML implementation is simple and designer-friendly:
<UserControl>
<StackPanel Padding="20">
<TextBlock>Lorem ipsum.</TextBlock> <CheckBox>I agree!</CheckBox> <Button>Submit</Button>
</StackPanel> </UserControl>
This XAML renders my earlier prototype and shows just how simple a user control can be. Of course, there’s no custom behavior yet, only the built-in behavior of the controls I declare.
The Text Fast Path EULAs are long, so let’s address text perfor- mance. The TextBlock (and only the TextBlock) has been optimized to use the fast path, low memory and CPU rendering. It’s built to
be fast—but I can spoil that:
<TextBlock Text="Optimized" /> <TextBlock>Not optimized</TextBlock>
Using TextBlock inline controls like <Run/> and <LineBreak /> breaks
optimization. Properties like CharacterSpacing, LineStackingStrategy
and TextTrimming can do the same. Confused? There’s an easy test:
Application.Current.DebugSettings .IsTextPerformanceVisualizationEnabled = true;
IsTextPerformanceVisualizationEnabled is a little-known debug setting that allows you to see what text in your application is opti- mized as you debug. If the text isn’t green, it’s time to investigate.
With every release of Windows, fewer properties impact the fast path; however, there are still several that negatively and unexpectedly impact performance. With a little intentional debugging, this isn’t a problem.
Immutable Rules There are as many opinions as there are options for where business logic should reside. A general rule puts less-mutable rules closer to the control. This is generally easier and faster and it optimizes maintainability.
You decide to start with a user con- trol. Perhaps it can get the job done. Spoiler alert: It can’t.
User Controls
User controls are easy. They provide consistent, reusable interfaces and a custom, encapsulated codebehind. To create a user control, select User Control from the Add New Item dialog, as shown in Figure 2.
User controls are usually a child of another control. However, their lifecycle is so similar to that of windows and pages that a user control can be the value set to the Window.Current.Content proper- ty. User controls are full-featured, supporting Visual State Manage- ment, internal resources, and every other staple of the XAML frame- work. Building them isn’t a compro- msdnmagazine.com
Figure 2 Add New Item Dialog
May 2019 23