I am trying to create a Visual Studio Item Template that will create a WPF Window with an attached file for a view model
Like the following
VMWindow.xaml ---VMWindow.xaml.cs ---VMWindow.vm.cs
I am able to create the template with the following .vstemplate file
<VSTemplate Type="Item" Version="2.0.0"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>Viewmodel Dialog Box</Name>
<Description>Viewmodel Dialog Box</Description>
<Icon>Icon.ico</Icon>
<ProjectType>CSharp</ProjectType>
<DefaultName>VMDialog</DefaultName>
</TemplateData>
<TemplateContent>
<ProjectItem TargetFileName="$fileinputname$.xaml" SubType="Window">ViewModelDialogTemplate.xaml</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.xaml.cs">ViewModelDialogTemplate.xaml.cs</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$.vm.cs">ViewModelDialogTemplate.vm.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
I would like for the template to create itself with the .vm.cs file nested inside the main Window file when displayed in Solution Explorer.
I have found the following howto, I am having trouble following it with Visual Studio 2010 though. It was written in 2008, does this still apply?
You need to implement the Microsoft.VisualStudio.TemplateWizard.IWizard interface, and write a little bit of code to remove the new item from the project and re-add it as the child of another item. Here is a working example from QueryFirst that takes any file with the extension .gen.cs and makes it a child of the same-named .sql file...
To attach the code to the template, you'll need something like this in your .vstemplate file...
There's a much easier way. You can pull in the same wizard that VS uses to construct composite items. You do this by adding an element at the end of your template, after <TemplateContent>...
Then you need to tell the wizard the extension of the parent, and the extension of the children...
This element goes inside <TemplateContent>.
This solution is tested and working in VS2012, and you can see the version hardcoded in the call to the wizard. If you have version problems, look for the file webform.vstemplate (visual studio's .aspx template), and inspire yourself.
It's actually very easy...
As it turns out the same method works for VS 2010. Required a bit of adaptation but this Code Project article covers the basic idea.