【问题标题】:Which Compile elements are required in SDK-format CSPROJ?SDK 格式的 CSPROJ 需要哪些编译元素?
【发布时间】:2020-08-11 16:49:07
【问题描述】:

我正在为我的 C# 项目使用“新”SDK 格式项目样式。我仍在使用 .NET Framework 4.8,它是一个 winforms 应用程序。我已经这样定义了项目:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <UseWindowsForms>true</UseWindowsForms>
    <TargetFramework>net48</TargetFramework>
    <!-- etc... -->
  </PropertyGroup>
</project>

我注意到,没有任何明确的 CompileEmbeddedResource 元素,考虑到 *.cs*.resx 文件(分别),我的 Form.csForm.Designer.csForm.resx 文件被正确分组在Visual Studio 2019。

它们在解决方案资源管理器中按如下方式列出:

+ Form.cs
  - Form.Designer.cs
  - Form.resx

然而,奇怪的是资源文件本身(Properties/Resources.resx 的那个)没有被适当地分组:

+ Resources.Designer.cs
+ Resources.resx

它们都显示为同级文件而不是嵌套的。如果我将此 XML 放入 csproj:

  <ItemGroup>
    <Compile Update="Properties\Resources.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
  </ItemGroup>

然后我在解决方案资源管理器中得到预期的层次结构:

+ Resources.resx
  - Resources.Designer.cs

资源文件是否需要Compile 元素,或者这是一个错误?这里的正确设置是什么?

请注意,在最新版本的 Jetbrains Rider 中,它显示了两个更改之间的正确嵌套;只有 Visual Studio 2019 显示出层级差异。

我无法在 MSDN 上找到任何关于此的特定文档。我知道this page,但它似乎没有专门针对这个话题。

测试的版本:

  • 骑士:2020.1.4
  • Visual Studio:16.6.5

【问题讨论】:

    标签: c# msbuild csproj


    【解决方案1】:

    SDK 项目只生成默认的&lt;Compile Include="*\*.cs /&gt; 元素。您的元素正在通过使用&lt;Compile Update="Properties\Resources.Designer.cs"&gt; 添加DesignTimeAutoGenDependUpon 属性来更改默认行为。

    关于系统生成的&lt;Compile /&gt;元素的更多信息可以在这里找到:https://docs.microsoft.com/en-us/dotnet/core/project-sdk/overview#default-includes-and-excludes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多