File attachments in an item template

I am trying to create a Visual Studio element template that will create a WPF window with an attached file for the view model

As below

VMWindow.xaml --- VMWindow.xaml.cs --- VMWindow.vm.cs

I can create a 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 the template to create itself with the .vm.cs file embedded in the main window file when displayed in the solution explorer.

I found the following: I am having problems with Visual Studio 2010. It was written in 2008, is it still applied?

Draft Article Code

+1
source share
4 answers

As it turns out, the same method works for VS 2010. It takes a bit of customization, but this code draft article covers the main idea.

0
source

Actually it’s very simple ...

  <ProjectItem TargetFileName="$fileinputname$.xaml" SubType="Window">ViewModelDialogTemplate.xaml</ProjectItem> <ProjectItem TargetFileName="$fileinputname$.xaml/$fileinputname$.xaml.cs">ViewModelDialogTemplate.xaml.cs</ProjectItem> <ProjectItem TargetFileName="$fileinputname$.xaml/$fileinputname$.vm.cs">ViewModelDialogTemplate.vm.cs</ProjectItem> 
+6
source

It’s much easier there. You can use the same wizard that VS uses to create composite elements. You do this by adding an element at the end of your template, after <TemplateContent> ...

 <WizardExtension> <Assembly>Microsoft.VisualStudio.Web.Application, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly> <FullClassName>Microsoft.VisualStudio.Web.Application.WATemplateWizard</FullClassName> </WizardExtension> 

Then you need to tell the wizard the parent extension and the extension of the children ...

 <CustomParameters> <CustomParameter Name="$ParentExtension$" Value=".xaml"/> <CustomParameter Name="$ChildExtension$" Value=".cs"/> </CustomParameters> 

This element goes inside the <TemplateContent>.

This solution is tested and works in VS2012, and you can see the version that is hard-coded when the wizard is called. If you have version problems, find the webform.vstemplate file (visual studio .aspx template) and inspire yourself.

+2
source

You need to implement the Microsoft.VisualStudio.TemplateWizard.IWizard interface and write some code to remove a new element from the project and re-add it as a child of another element. Here is a working example from QueryFirst that takes any file with the extension .gen.cs and makes it a child with the same name .sql file ...

  public void ProjectItemFinishedGenerating(ProjectItem item) { string path = item.FileNames[0]; string parentPath = null; if (path.EndsWith(".gen.cs")) parentPath = path.Replace(".gen.cs", ".sql"); if (path.EndsWith("Results.cs")) parentPath = path.Replace("Results.cs", ".sql"); if (!string.IsNullOrEmpty(parentPath)) { ProjectItem parent = item.DTE.Solution.FindProjectItem(parentPath); if (parent == null) return; item.Remove(); parent.ProjectItems.AddFromFile(path); } } 

To attach code to a template, you will need something similar in your .vstemplate file ...

 <WizardExtension> <Assembly>QueryFirst, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4688a60b10e39f04</Assembly> <FullClassName>QueryFirst.WizardImplementation</FullClassName> </WizardExtension> 
0
source

Source: https://habr.com/ru/post/1481469/


All Articles