XAML Regions: A Visual Studio 2010 Extension

If you’re like me, dealing with giant XAML files is problematic. I don’t seem to possess the mental stack that some XAMLers have that allows them to deal with pages upon pages of disorganized XAML code. I frequently run into this dilemma when I deal with WPF themes. They’re usually colors, brushes, styles and templates for every single WPF control in existence all crammed into one file. I have to rely on text searches for terms like “Button” to find what I’m looking for. I found myself pleading to the markup language gods for the regioning system I had grown so accustomed to in C#. My prayers combined with my Internet searches lacked the fecundity to bare any useful results, so I was on my own. Luckily, Visual Studio 2010 has a pretty easy-to-use Extension Framework. As such, I whipped up a plugin that allows XAML regions. Like most things of this nature, I see no pressing reason to hoard it all to myself.

XAML Regions Extension For Visual Studio 2010

XAML Regions Extension For Visual Studio 2010

Using The Extension

There’s nothing particularly complex about using this extension. I wanted to make it so that people without this extension didn’t have trouble working with code from people who did have the extension. I chose to make it so that XAML regions were defined in XML comments. One would start a XAML region by having a few lines that consist of

1
2
3
<!-- Region (Any Text You Want) -->
Your Code
<!-- EndRegion -->

Spacing, prefix hash characters, and description text are all generally optional. All of the following uses are supported:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- Region Hello World -->
Your code
<!-- End Region -->
 
<!-- RegionHello World -->
Your code
<!-- EndRegion -->
 
<!-- #Region Hello World -->
Your code
<!-- #End Region -->
 
<!-- #Region Hello World -->
Your code
<!-- #EndRegion -->
 
<!-- Region Hello World -->
Your code
<!-- End Region Hello World -->

That’s really all there is to it!

Grab it at the Visual Studio Gallery

Updated in 0.4

  1. Added the ability to have hash character prefixes (e.g., <!– #Region –>)
  2. IAdded the ability to have description text in the EndRegion area (e.g., <!– End Region (Your Text) –>)

Enjoy!

  16 comments

  1. Evgeny   •  

    That’s what was lacking! Thanks for sharing. Nice work.
    Can you please add another possibility to specify the name of the region at the end.
    For example:


    Thanks.

  2. Evgeny   •  

    For example:
    <! – Region Header ->


    <! – EndRegion Header ->

  3. Jacob   •     Author

    Good idea! I’ll do that! It should improve the ability to read these when expanded. Also going to add the ability to include the # sign in there (#Region). Should push out a new version in the next day or so.

  4. Stephanie   •  

    Tried just about everything–including copying/pasting your “Hello World” examples and re-opening file–it just won’t work for me. Also checked the Extension Manager and restarted VS2010.

    I’m wondering if the code-behind has to be C#? If so, would it be possible to update at some point to allow VB?

    Thanks!

  5. Jacob   •     Author

    Curious – you’re actually my first bug report (on this project)! I just tested it with a VB project and it works fine on my end. The extension should be fine with any sort of XAML file (meaning WPF/Silverlight, VB/C# code-behind, no code-behind, etc).

    Are you using a non-express version of Visual Studio (Microsoft won’t allow extensions that modify the code editor to work in Express editions)? If you’d like, hit me up with an email through the “Contact” section at the top and we can try to debug this.

  6. Vitor Canova   •  

    It is amazing. Very good work. I hope MS will introduce something like that in VS201*

  7. TJ   •  

    This isn’t working for me. VS2010, WPF .net4.0, C#.. Pasted all the examples and nothing.

  8. TJ   •  

    Just found that VSCommands 2010 will cause the plugin not to work. I tried disabling all the option in VSCommands and couldn’t get it to work, but when you disabled VSCommands XAML Regions works just fine….

  9. Jacob   •     Author

    Thanks for the heads up, TJ. I’ll try to track down the conflict between my plugin and VSCommands.

  10. DK   •  

    I was wondering, do you have the source available for your Visual Studio extension? I’m a VB programmer, and I’d like to see the following (VB-double-quote-style) supported:

    <!– #Region ” Copyright (c) Pearson plc ” –>
    Your Code
    <!– #End Region –>

    With the source, I could do it myself…
    Thanks
    Dave

  11. Spoelders   •  

    Very good. Works fine in VS2010, WIN7, Framework 4.0
    Thanks!

  12. Pavel   •  

    Extremely useful extension.

  13. shemesh   •  

    hi, GREAT WORK!
    i have de-compiled your code and changed the regex expressions to make a “new” extension to allow collapsing xaml comments.
    nowadays when commenting xaml it cannot be collapsed, with this small change to your original code, it is now possible!

    here is the new regex:
    private readonly Regex _startRegionText = new Regex(““, RegexOptions.IgnoreCase);

    would you please post it?

  14. spi   •  

    Hi,

    Thank you very much for this extensions, it’s very useful !! :)
    I have one question : This is possible to add a button to ‘expand/collapse’ all of the regions, please ? It would be very useful !!

    Best regards.

  15. Jacob   •     Author

    Hey spi, thanks for the suggestion. Unfortunately, there are a lot of other plugins out there that expand and collapse all regions that should work with this extension. I know Microsoft’s own Productivity Power Tools is one of them. I feel like if I added that, I would just be duplicating functionality for most people.

  16. Don   •  

    I have enjoyed and used this tool since finding it when Visual Studio 2012 came out. Now I am on 2013 and I am sorry to say the regions seem to gradually slow down the XAML pages of my solution until suddenly it almost sees locked up! By removing other add-ins one by one, I have come to the conclusion it is your tool. I hate this!! Any chance you have already heard about this and are working on a fix?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>