Skip to content

[Question] Multi-file Item Templates #99

Closed

Description

@dazinator

I have created a "multi-item" template, it's for an ascx file, so that when the user adds the ascx file to the project, it adds the designer and the code behind file as well.

This is working in that it adds the files, but they don't end up nested together in solution explorer, e.g they end up looking like this:

image

I followed these docs when defining the vstemplate file:

  <TemplateContent>
    <References>
      <Reference>
        <Assembly>System</Assembly>
        <Assembly>System.Web</Assembly>
      </Reference>
    </References>
    <ProjectItem SubType="" TargetFileName="$fileinputname$.ascx" ReplaceParameters="true">View.ascx</ProjectItem>
    <ProjectItem SubType="ASPXCodeBehind" TargetFileName="$fileinputname$.ascx.cs" ReplaceParameters="true">View.ascx.cs</ProjectItem>
    <ProjectItem SubType="" TargetFileName="$fileinputname$.ascx.designer.cs" ReplaceParameters="true">View.ascx.designer.cs</ProjectItem>   

  </TemplateContent>

Is there something I could be missing?

Activity

  1. aienabled commented on Dec 29, 2015

    @aienabled

    This way you just adding these items to the project file, but the project system doesn't know how to properly process them (that these files should be nested, compiled with specific build tool, etc).

    What you need to do, is define custom item type. However the tutorial by this link doesn't describe how to add support for nested/dependent files.

    I will post an with example of adding XAML Page item type to your project system (which has dependent .xaml.cs file and displayed as nested, it will also participate with MSBuild if you import common CSharp targets). You could implement support for ASCX items in a similar way.

    First, you need to define a Xaml Rule to acknowledge Project System about the new item type:
    (place into BuildSystem/Rules/PageXaml.xaml (better to use a Xaml Rule item template for this))

    <Rule
        Name="PageXaml"
        DisplayName="PageXaml"
        PageTemplate="tool"
        Description="PageXaml build items"
        xmlns="http://schemas.microsoft.com/build/2009/properties">
        <Rule.DataSource>
            <DataSource Persistence="ProjectFile" HasConfigurationCondition="True" ItemType="Page" />
        </Rule.DataSource>
    
        <StringProperty Name="Identity" DisplayName="File Name" ReadOnly="true" Category="Misc">
            <StringProperty.DataSource>
                <DataSource Persistence="Intrinsic" ItemType="Page" PersistedName="Identity" />
            </StringProperty.DataSource>
        </StringProperty>
    
        <StringProperty Name="FullPath" DisplayName="Full Path" ReadOnly="true" Category="Misc">
            <StringProperty.DataSource>
                <DataSource Persistence="Intrinsic" ItemType="Page" PersistedName="FullPath" />
            </StringProperty.DataSource>
        </StringProperty>
    
        <BoolProperty Name="Visible" Visible="False" />
        <StringProperty Name="DependentUpon" Visible="false" />
        <StringProperty Name="Link" Visible="false" />
    </Rule>
    

    Second, you need to edit ProjectSchemaDefinitions.xaml and define ContentType, ItemType and FileExtension:

        <!-- XAML -->
        <ContentType
            Name="PageXaml"
            DisplayName="XAML Page"
            ItemType="Page">
            <NameValuePair Name="DependentFileExtensions" Value=".cs" />
            <NameValuePair Name="DefaultMetadata_SubType" Value="Designer" />
            <NameValuePair Name="DefaultMetadata_Generator" Value="MSBuild:Compile" />
        </ContentType>
    
        <ItemType Name="Page" DisplayName="Page" />
    
        <FileExtension Name=".xaml" ContentType="PageXaml" />
    

    (please note that here used "DependentFileExtensions" pair, it defines files nesting).

    Third, you need to edit your targets-file to add support for properties browser for this item type:

    <PropertyPageSchema Include="$(MSBuildThisFileDirectory)Rules\PageXaml.xaml;">
        <Context>File;BrowseObject</Context>
    </PropertyPageSchema>
    

    If you done this correctly, you could try to add existing UserControl XAML-file with its code-behind .XAML.CS-file and it will properly appear into the project tree and will be built by MSBuild (if you import common CSharp targets).

    Regards!

  2. dazinator commented on Dec 29, 2015

    @dazinator
    Author

    Vladimir Kozlov (@aienabled) - wow thank you so much for your help with this and putting me on track. I'll give this a go! :-)

  3. dazinator commented on Dec 29, 2015

    @dazinator
    Author

    Vladimir Kozlov (@aienabled) - I have go to this part:

    Second, you need to edit ProjectSchemaDefinitions.xaml and define ContentType, ItemType and FileExtension:

    My project does not have this rule file. Should it be there by default, or do I just need to create this file? These are the rules in my project that were created by default:

    image

  4. dazinator commented on Dec 29, 2015

    @dazinator
    Author

    Vladimir Kozlov (@aienabled) ignore me - I can see that this file corresponds to the ProjectItemsSchema.xaml file from the link you provided

  5. dazinator commented on Dec 29, 2015

    @dazinator
    Author

    Part way there!

    image

    Just need to figure out how get the designer file nested under the .cs file.

  6. dazinator commented on Dec 29, 2015

    @dazinator
    Author

    This is working nicely.

    I would also like the .cs file to be given a SubType of ASPXCodeBehind so it ends up looking like this in the project file - but I haven't figured out how to do that yet, as I can't find a place in the xaml to specify the SubType.
    All in all I'm after this in the project file:

     <ItemGroup>
            <Content Include="WebUserControl1.ascx" />
      </ItemGroup>
      <ItemGroup>
         <Compile Include="WebUserControl1.ascx.cs">
          <DependentUpon>WebUserControl1.ascx</DependentUpon>
          <SubType>ASPXCodeBehind</SubType>
        </Compile>
        <Compile Include="WebUserControl1.ascx.designer.cs">
          <DependentUpon>WebUserControl1.ascx</DependentUpon>
        </Compile>
      </ItemGroup>

    I'll raise a seperate issue for this if I can't figure it out.

  7. dazinator commented on Dec 29, 2015

    @dazinator
    Author

    Nope no joy. I'll raise a more concise issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions