在 csproj 中使用通配符时如何在 xaml 文件下嵌套 xaml.cs

How to nest xaml.cs under xaml files when using wildcards in csproj

我对 csproj 文件有疑问。

因为我生成了一个很大的源代码库,所以我试图尽量减少项目文件的冲突(与团队一起开发时)。

我设法通过使用这样的标签摆脱了大多数冲突:

<Compile Include="Services\**\*.cs" /> 

对于普通的 cs 文件和

<Page Include="Entities\**\*.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
</Page>

用于 xaml 和代码隐藏。

不幸的是,我松开了 .xaml 和 .xaml.cs 对的分组,这是可以忍受但笨拙的。

有什么方法可以为这些创建通用模式吗?

<Compile Include="MainWindow.xaml.cs">
  <DependentUpon>MainWindow.xaml</DependentUpon>
  <SubType>Code</SubType>
</Compile>

我知道拆分 csproj 本身的可能性,但这会禁止解决方案范围内的搜索,所以这是不行的

As I have a big source base that is generated

您不能也生成或至少更新 csproj 文件吗?

这就是我为 99% 生成的程序集所做的。我将所有手动工作放在一个单独的文件夹中,该文件夹在生成器中枚举并添加到生成项旁边的项目文件中。

使用 MSBuild 15 的静态更新语法,您可以做到

 <Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />

这里的技巧是MSbuild将路径分为Filename (foo.xaml.cs => foo.xaml) 和Extension (foo.xaml.cs => .cs), 所以 %(Filename) 可以用来引用 xaml 文件。