Extending Visual Studio Part 3 - Item Templates. This article is part of the series 'Extending Visual Studio'. In part three of my series on Extending Visual Studio, we're going to take a look at Item Templates. Item Templates are what you see when you choose the menu option 'Add New Item': Here we can see a stack of Item Templates. Some come with Visual Studio, some are my own. It's surprisingly straightforward to add your own custom Item Templates, but the documentation on this is a little scant. In this article we'll put together a band new Item Template for a 'View Model' - a class that implements INotify. Property. Changed. Please avoid picking the View Model to death - it's just to give us a better task that 'Hello World'! For those of you who are interested, I have created some much more functional Item Templates for MVVM systems, more on that later. You will need the Microsoft Visual Studio SDK. I'm using Microsoft Visual Studio 2. SP1, be aware that there are different installers for 2. Creating Visual Studio project templates. Posted on 2013/06/09 by Uli Weltersbach. Creating Visual Studio Project Templates and Item. Creating custom templates in visual studio & resharper. I would like to create custom templates for c# files. Visual Studio templates are simple enough to. How to create new category for custom templates. There are also Visual Studio projects. SP1. Grab the SDK from the links below: Once you've installed the SDK you'll have a whole host of new Item Templates and Project Templates to play with - filed under 'Visual C# > Extensibility': To get started, open Visual Studio, choose 'New Project' and select 'C# Item Template' Call the project 'View. Model. Item. Template'. There's very little to it. View. Model. Item. Template. ico - The icon for the item template. View. Model. Item. Template. vstemplate - The template metadata. Class. cs - The file the template will use as a base. Firstly let's rename 'Class. Creating custom project templates provides you with. Create custom project templates in Visual Studio. We can create custom project templates.
View. Model. cs'. Now we'll open the vstemplate file and update the metadata: Blow by blow, here's what we're defining with the metadata (the Template. Data item): The Template. Content is a bit more straightfoward in this example. We state that 'System' is an assembly that a reference is required to for this item, and that we're going to take the item 'View. Model. cs' from this project and replace parameters in it (replacements we'll take a look at in a bit). Building the Template Itself. Building the template itself is dead easy - you just use a few of the predefined tokens with dollar signs around them. Here's what the View. Model. cs template looks like: using System. System. Collections. Generic. $if$ ($targetframeworkversion$ > = 3. System. Linq. $endif$using System. Text. using System. Component. Model. We have the item template ready to go. The next thing. to do is to create a VSIX package which will contain the template. We can also. use this as a way to test and debug it. Creating the VSIX Package. In the solution, add a new project called 'View. Model. Template. Vsix' with the type. VSIX project. Double click on the 'source. Here you can edit data. We'll set a better title and some descriptions, as. Now we can press 'Add Content' to include our Item Template: Now that we've done this make sure the VSIX package is the startup project and. F5. This will start the Experimental Instance of Visual Studio which is. Create a new C# Class Library project. Add New Item..' our new View. Model item template is listed - . Example' as the file name and add the item. Now this is most impressive - we have got a new class with the correct name that. INotify. Property. Changed! Here's the newly generated class: using System. System. Collections. Generic. using System. Linq. using System. Text. using System. Component. Model. Class. Library. 2. At this stage you now know how to build a basic item template. In the next part of this article. Wizard for the Item Template that'll let us. You may not just want to create a new item with a. In this case. you need to include a Wizard for the Item Template. We're going to allow. Lay out the form as below. I'm not going to go into the details of how the form works, it's very simple - . Include Example' as a boolean property as well as the. Example Name and Example Type as strings. We now need to actually create the. Wizard class. Add a new class to the project called 'View. Model. Wizard'. At this. ENVDTE Microsoft. Visual. Studio. Template. Wizard. Interface. The Wizard class must implement 'IWizard'. Right click on the 'IWizard' line and. Implement Interface'. You're class should now look like this: publicclass View. Model. Wizard : IWizard. This will. let us use the tokens in our template. Update the class to look like this: publicclass View. Model. Wizard : IWizard. The dictionary can only contain strings - so use. We'll set up a conditional block based on $Include. Example$ that will add. Property. Name$ and type $Property. Type$ that uses. Notify. Property. Changed. Update View. Model. System. using System. Collections. Generic. System. Linq. $endif$using System. Text. using System. Component. Model. The post build step will install the wizard to the. GAC and show the strong name token: Assembly successfully added to the cache. Microsoft (R) . NET Framework Strong Name Utility Version 3. Copyright (c) Microsoft Corporation. Open the. View. Model. Item. Template. vsitemtemplate file and after the closing tag of. Template. Content add: -- > < Wizard. Extension> < Assembly>. View. Model. Wizard, Version=1. Culture=neutral, Public. Key. Token=e. 07. Assembly> < Full. Class. Name> View. Model. Wizard. View. Model. Wizard< /Full. Class. Name> < /Wizard. Extension>. Don't forget to use the correct public key token (highlighted in bold). Add a new View Model to a class library - we get the user. System. Component. Model. namespace Class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |