1

In order to programmatically include a file to a project I use the method provided in this answer https://stackoverflow.com/a/23537007/1317323

Now I need to go further and add a xaml file and xaml.cs file to my project.

I need to know what would be an accepted string in the method AddItem() in order to include both files the right way. Something like

p.AddItem("Page", "so.xaml");
// or
p.AddItem("Xaml", so.xaml");

When I use the classic visual studio "right click/ add new item" way, I get this in the csproj file

<ItemGroup>
  <Page Include="example.xaml">
    <SubType>Designer</SubType>
    <Generator>MSBuild:Compile</Generator>
  </Page>
</ItemGroup>
<Compile Include="example.xaml.cs">
  <DependentUpon>example.xaml</DependentUpon>
</Compile>

I read msdn with no luck.

I'm loking for a table that would give the value a the first parameter for a given item type or a way to construct such a table.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Yvain
  • 882
  • 1
  • 10
  • 27
  • If it's a XAML file, and the root tag has an `x:Class` attribute, there's probably a .xaml.cs file -- but I can't say if you can guarantee it'll have the same name except for the ".cs" suffix. There isn't a grand table of these things, or a general way to deduce anything from first principles. When you add a UserControl (*for example*) to a WPF project, the wizard creates .xaml and .xaml.cs. The wizard that creates a Window does the same. When you add a resource dictionary, that one only adds .xaml. Personally, when user adds "foo.xaml", I'd *look for* "foo.xaml.cs". – 15ee8f99-57ff-4f92-890c-b56153 May 23 '17 at 13:11
  • ...or when adding "foo.bar", I might look for "foo.bar.*". Maybe. – 15ee8f99-57ff-4f92-890c-b56153 May 23 '17 at 13:12

1 Answers1

0

I was able to do it using another namespace from Microsoft.

The solution is based on this one

Instead of manipulating my solution with

 Microsoft.Build.Evaluation.Project

I use

Microsoft.VisualStudio.Shell.Interop

Now I manipulate

EnvDTE.Project

And I can use

_currentProject.ProjectItems.AddFromFile(path);

If I do it twice with a xaml and a xaml.cs file with the same name, there are automatically nested.

Yvain
  • 882
  • 1
  • 10
  • 27