Frage

I'm trying to create a custom item template for Visual Studio that creates multiple files. Two of those files are Resource.resx file and a corresponding 'Resource.Designer.cs' file.

I've managed to display them as a parent-child files with this declaration in the .vstemplate file:

<ProjectItem ItemType="EmbeddedResource" TargetFileName="$fileinputname$Resources.resx" ReplaceParameters="true">Resources.resx</ProjectItem>
<ProjectItem TargetFileName="$fileinputname$Resources.resx\$fileinputname$Resources.Designer.cs" ReplaceParameters="true">Resources.Designer.cs</ProjectItem>

This also declared Resource.resx as an embedded resource.

The problem I'm getting now is that the Resource.resx file doesn't use Resources.Designer.cs as a code behind file after creating item with this template. As soon as I add a new resource string in Resource.resx file, it creates a new Resources1.Designer.cs file which Visual Studio uses as a code behind from that point.

I've examined the changes made in .csproj file before and after Visual Studio creates a new code-behind file and I found that the main reason is because there were no <LastGenOutput> entries for Resource.resx file.

Is there some way to declare <LastGenOutput> through .vstemplate or any other way to connect .resx file with it's code-behind file without doing it manually?

War es hilfreich?

Lösung

It isn't very clear why you need this template since it is already included with Visual Studio. You select it with Project + Add New Item + General + Resources File.

If you want to customize it then your best bet is to get started with the existing one. Navigate to C:\Program Files (x86)\Microsoft Visual Studio x.x\Common7\IDE\ItemTemplatesCache\CSharp\General\1033\Resource.zip where x.x is the VS version number and 1033 might be different if you don't use the English version of VS.

I'll just post the .vstemplate file you'll find there, be sure to copy/paste the file to get started on your own:

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2390" />
    <Description Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2391" />
    <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4565" />
    <TemplateID>Microsoft.CSharp.Resource</TemplateID>
    <ProjectType>CSharp</ProjectType>
    <RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
    <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
    <DefaultName>Resource.resx</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem>Resource.resx</ProjectItem>
    <CustomParameters>
      <CustomParameter Name="$itemproperties$" Value="CustomTool" />
      <CustomParameter Name="$CustomTool$" Value="ResXFileCodeGenerator" />
    </CustomParameters>
  </TemplateContent>
  <WizardExtension>
    <Assembly>Microsoft.VisualStudio.Editors, Version=9.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
    <FullClassName>Microsoft.VisualStudio.Editors.ResourceEditor.ResxItemWizard</FullClassName>
  </WizardExtension>
</VSTemplate>

If you want to add more files then add more <ProjectItem> elements.

Andere Tipps

The parent-child relation should be declared using the DependendUpon tag in the C# project file (.csproj), like this:

<Project ...>
   ...
    <ItemGroup>
      ...
        <EmbeddedResource Include="Resources.resx">
           ...
        </EmbeddedResource>
        <Compile Include="Resources.Designer.cs">
            <DependentUpon>Resources.resx</DependentUpon>
            ...
        </Compile>
        ...
    </ItemGroup>
 </Project>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top