【问题标题】:How to add a glob for resx files for new SDK csproj file如何为新的 SDK csproj 文件添加 resx 文件的 glob
【发布时间】:2017-10-05 11:28:28
【问题描述】:

如果我在 VS2017 的新 dotnetstandard 2.0 SDK 项目中的属性文件夹中添加一个新的 resx 文件,我会看到

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <EmbeddedResource Remove="Properties\foo.resx" />
  </ItemGroup>

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

  <ItemGroup>
    <EmbeddedResource Update="Properties\MyWords.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>MyWords.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>


</Project>

但是,我更希望以与处理普通 cs 文件相同的方式处理它。项目为空,搜索文件系统。实现上述目标的 globby 方法是什么,这样当我添加新文件时,它们最终不会被明确声明。

我的第一次尝试是

  <ItemGroup>
    <Compile Update="Properties\**\*.designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>Properties\%(Filename).resx</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="Properties\**\*.resx">
      <Generator>ResXFileCodeGenerator</Generator>

    </EmbeddedResource>
  </ItemGroup>

但这行不通,因为

Properties\%(Filename).resx

扩展到

Properties\Foo.designer.resx

而不是

Properties\Foo.resx

【问题讨论】:

  • 致正在阅读本文的人:资源文件包含在新的 SDK csproj 中。见这里:docs.microsoft.com/en-us/dotnet/core/tools/…。不需要这个:)
  • 你想添加一个答案吗?如果允许我这样做,我可以切换接受的答案。
  • 我稍后会添加,但无需更改。当时的答案是这样的:)。
  • 这可能对当时提供正确信息的人不公平,但将答案转换为更新的解决方案将极大地帮助所有发现此问题的人。跨度>

标签: .net msbuild


【解决方案1】:

感谢@stijn 提供的指导,我们将这个解决方案更进一步,因为它缺少我们在发布期间使用的EmbeddedResource 步骤,以避免运行时的磁盘IOPS。

这是一个类似的策略,也适用于嵌入式资源:

RESX + 嵌入式资源(MSBUILD 15 Glob 样式)

  <ItemGroup>
    <Compile Include="**\*.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>$([System.String]::Copy('%(FileName)').Replace('.Designer', '.resx'))</DependentUpon>
    </Compile>
    <EmbeddedResource Include="**\*.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>$([System.String]::Copy('%(FileName)')).Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>

备注MSBUILD Item Metadata reference is a great resource - 例如它告诉你%FileName 不包括最初让我绊倒的扩展名。还有this related SO post affirms the necessity of using String.Copy

【讨论】:

  • 注意,如果你想遵循这个(至少对于 .NET Core SDKs),并且希望通过 Directory.Build 文件进行配置,你需要把它放在 Directory.Build.targets而不是 Directory.Build.props
【解决方案2】:

您可以对元数据使用属性函数,因此使用 String.Replace 擦除 .Designer 部分应该没问题:

<Compile Update="Properties\**\*.designer.cs">
  <DesignTime>True</DesignTime>
  <AutoGen>True</AutoGen>
  <DependentUpon>Properties\$([System.String]::Copy('%(FileName)').Replace('.Designer', '')).resx</DependentUpon>
</Compile>

【讨论】:

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