Other, Xamarin.Forms

[XAML] Tip of the day : #region

Hi Developpers !

It’s been a long time since I wrote an article.
But it’s time to get back on track!

And to start with, I’m going to present you a XAML feature that I missed during all these years.

“#region” in C#, we all know, It has good and bad sides.
Well I can tell you that you can do the same thing in XAML.

There are plenty of different opinions on this. Personally, I find regions quite useful if used with parsimony. Especially when discovering code from a colleague or some old code from me.

💡 If you need to group some parts of your code using regions, it’s probably time for some refactoring.

Let me show you what happens with some code :

   			<!--#region Header part-->
            <Label
                Grid.Row="0"
                FontSize="32"
                HorizontalOptions="Center"
                SemanticProperties.HeadingLevel="Level1"
                Text="Hello, World!" />

            <Label
                Grid.Row="1"
                FontSize="18"
                HorizontalOptions="Center"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                SemanticProperties.HeadingLevel="Level1"
                Text="Welcome to .NET Multi-platform App UI" />
            <!--#endregion-->
            <!--#region Footer part-->
            <Label
                x:Name="CounterLabel"
                Grid.Row="2"
                FontAttributes="Bold"
                FontSize="18"
                HorizontalOptions="Center"
                Text="Current count: 0" />
            <!--#endregion-->

Shows grouped as :

The tooltip is always there to give us information within the region if necessary

The xaml regions obviously work with UWP , WPF , Xamarin.Forms , MAUI etc …

Happy to be back 😎

It is with great pleasure that I was able to take the time to write a few lines.

I look forward to seeing you in a future article

Good development

Leave a Reply

Your email address will not be published. Required fields are marked *