Search code examples
wpfxamlxamarinuwpconventions

Is it safe in general to use spaces between specific thickness values (e.g. Margin or Padding) in XAML elements?


Recently one of my colleagues started using spaces instead of commas as separators for specific values in Thickness fields like Margin and Padding:

<Label Margin="0 1 2 3"/>

While previously we used commas:

<Label Margin="0,1,2,3"/>

He says he saw a lot of usage of this style in XAML examples across the web, but could not provide any of them quickly. His code is working in .NET 6 project with WPF, but we have another projects with different .NET targets so I started digging if this style is acceptable is general.

Some parts of WPF documentation restrict usage to commas only:

The four values must be separated with commas; spaces are not allowed.

General article about Thickness in WPF does not mention separators at all (as for .NET Framework 4.8 and above), but provides an example with a starting whitespace, so I assume it is is OK:

<object property=" left,top,right,bottom" ... /> 

Xamarin docs includes an example with both commas and spaces:

<Label Text="Xamarin.Android" Margin="0, 20, 15, 5" />

Although similar article for MAUI suggests an example without spaces and does not mention them:

 <Label Text=".NET Android" Margin="0,20,15,5" />

Finally UWP docs explicitely says that commas and spaces are allowed to use, and one user even created an issue for WinUI3 when caught a problem with spaces.

So,

  • Is it safe to use spaces in XAML in general (i.e. use it by default for projects targeting any .NET version or framework)?
  • Is it allowed to mix them (like it was suggested by Xamarin example)? Will any preview engine (like in VS or JetBrains Rider) parse them correctly?
  • (That is a matter of opinion, but I still want to ask) What style is considered the most readable and/or convinient?

Solution

  • I have created all the differents project you mentioned. I found that:

    1. In the xamarin of the maui project, you must use the , to separate the values. If you use the space, it will throw the error (can't convert value "1 1 1 1" to Thickness).
    2. In the WPF and the WinUI project, you can use the both , and space to separate the values.

    In addition, if you both have , and space in the values, the space seems will be ignored. So I suggest you use the , to do that. Because the constructor of the Thickness class also used the ,. Such as new Thickness(left,top,right,bottom);