I’m not going to deny it. I’m a fan of ReSharper. One of it’s many feature is the ability to create your own code templates.
Whilst busy with a Xamarin Forms project. I needed a quick way to surround blocks of XAML code with a region.
Yes, you can add regions to XAML code. To give you an idea of what I mean, here is a small example of a region in XAML:
So, let’s quickly go through the steps of creating a ReSharper template to surround any piece of XAML code with the correct markup for a region.
- In Visual Studio, select ReSharper > Tools > Templates Explorer…
- Click on the Surround Templates Tab.
- Click the New Template toolbar button.
Add the following code to the template editor window:
Set the following properties for the template:
Shortcut: region
- Description: Surrounds XAML with a region
Mnemonic: 2
Make sure the Reformat and Surrounding checkbox and radio button is selected.
- Click on the “link” text next to availability and check the “In File matching: *.*” checkbox.
- Set the File Mask to *.xaml
- Lastly, check the “Show in context action” checkbox and save the template.
The final result should look like the following:
Now, when you select a piece of XAML code, you should see region inside the ReSharper “Surround with” context menu:
The result will be the code nicely wrapped inside a region, which can be collapsed to help with readability:
Thank you for reading. Until next time, keep coding!