Lesser known technologies in Xaml

Introduction

This article is going to introduce some lesser known technologies about the Xaml file.


Detail

The original link is XAML 很少人知道的科技 - walterlv. But this blog was written in Chinese, so I translate its content to English. Waterlv is an MVP(Microsoft Most Valuable Professional), and he is good at .NET Core\WPF\.NET. Here is his Blog.


The value of Thickness can be split by space.

When we set the value of FrameworkElement‘s Margin property at Xaml designer, we always use a comma to split it.

But have you tried to use space to split the value of Thickness ?

Here is an example

1
<Button Margin="10 12 0 0"/>

Set multiple values of enum with a comma(,).

If an enum was marked [Flags] attribute, it can be set multiple values with bit operation.

1
2
3
4
5
6
7
8
[Flags]
enum NonClientFrameEdges
{
Left = 0x001,
Right = 0x002,
Top = 0x003,
Bottom = 0x004
}

How to set the mutiple values of this enum? We can use , to fix this problem. Here is an example

1
<WindowChrome NonClientFrameEdges="Left,Bottom,Right" GlassFrameThickness="0 64 0 0" UseAeroCaptionButtons="False" />

Set multiple values of enum with +.

Usually, we use commas to set multiple values of enum.But, when we set the value of keyboard shortcuts in WPF/UWP , we use + instead of ,.

1
<KeyBinding Command="{x:Static WalterlvCommands.Foo}" Modifiers="Control+Shift" Key="W"/>

The type of Modifiers is ModifierKeys, and there is a special converter class that inherited TypeConverter. This class converts the value of Modifiers property (string) to enum. So, we use + to do bit operations for multiple values of keyboard shortcuts in Xaml.

Set value of XAML namespace with the type of Url.

The namespace of original controls in WPF/UWP is http://schemas.microsoft.com/winfx/2006/xaml/presentation; and the namespace of XAML’s designer is http://schemas.microsoft.com/winfx/2006/xaml. There are many XAML namespaces which were named with Url.

We can set namespace name to Url type by adding an attribute above current namespace.

1
2
using System.Windows.Markup;
[assembly: XmlnsDefinition("http://walterlv.github.io/demo", "Walterlv.NewCsprojDemo")]

For more information, you can visit this blog:

让你编写的控件库在 XAML 中有一个统一的漂亮的命名空间(xmlns)和命名空间前缀. This blog was written in Chinese, I will translate it later.

Set default value of namespace prefix in XAML.

The default value of XAML namespace in WPF/UWP is x. If we want to set default prefix for ourselves’s namespace, we can add a special Attribute to our namespace. Here is an example.

1
2
using System.Windows.Markup;
[assembly: XmlnsPrefix("http://walterlv.github.io/demo", "w")]

After you set this attribute, the XAML designer will use w as your prefix, when you add this namespace.

For more information,you can visit this blog:
让你编写的控件库在 XAML 中有一个统一的漂亮的命名空间(xmlns)和命名空间前缀.This blog was written in Chinese, I will translate it later.

It’s important to note that the defined component can’t be used in the same assembly!

Use your controls library without a namespace prefix in XAML

We write a usercontrol which’s name is DemoPage. Usually, we should add a namespace before we use it in XAML. But, there is a method to avoid adding a namespace before we use it.

1
2
3
4
5
6
7
8
<UserControl
x:Class="HuyaHearhira.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<DemoPage />
</Grid>
</UserControl>

The method is that we define the namespace as http://schemas.microsoft.com/winfx/2006/xaml/presentation.

1
2
using System.Windows.Markup;
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Walterlv.NewCsprojDemo")]

It’s important to note that the defined component can’t be used in the same assembly!