如何使用 VS 构建配置设置嵌入式资源 属性?
How to set an Embedded Resource property with VS Build Configurations?
我正在为 Mac 使用 Visual Studio。
我发现了这个 https://forums.xamarin.com/discussion/96777/how-to-set-info-plist-properties-with-vs-build-configurations,它描述了如何使用特定的构建配置设置辅助 plist 文件。效果不错..
<None Include="Entitlements.plist" Condition="'$(Configuration)' != 'Debug-Staging'" />
<None Include="Entitlements-staging.plist" Condition="'$(Configuration)' == 'Debug-Staging'">
<LogicalName>Entitlements.plist</LogicalName>
</None>
但是这种方法不适用于嵌入式资源...
<EmbeddedResource Include="ClientResources\myfolder\config.json"
Condition="'$(Configuration)' != 'Debug-Staging'" />
<EmbeddedResource Include="ClientResources\myfolder\config-staging.json"
Condition="'$(Configuration)' == 'Debug-Staging'">
<LogicalName>config.json</LogicalName>
</EmbeddedResource>
我也试过使用完整路径的 LogicalName...
将条件放在 ItemGroup 上也无济于事。
编辑:
这是我创建的示例项目...
https://github.com/JulesMoorhouse/ConfigBuildConfig/blob/master/ConfigBuildConfig/ConfigBuildConfig.csproj
如果选择 Debug-Staging
配置会发生什么,它应该读取 config-staging.json
并在控制台中输出值。
这些消息来源表明这应该有效...
- Same source, multiple targets with different resources (Visual Studio .Net 2008)
- https://social.msdn.microsoft.com/Forums/en-US/aa2d687d-0f6c-4a1c-af95-201f7e091f5c/embed-resource-conditionally-at-compiletime?forum=csharplanguage
这里似乎有两个主要问题。
- 您的 Debug-Staging 配置已映射到调试配置。
这意味着该条件未用于 EmbeddedResource。
如果在Solutionwindow中右击解决方案,然后selectOptions,然后selectBuild - Configurations,Configuration Mappings就可以看到映射了。
如果您在下拉列表中 select Debug-Staging,您可以看到它在构建时映射到调试。
作为这里的快速修复,我删除了 Debug-Staging 配置,re-added 它,确保检查所有解决方案项目的创建配置,然后映射 Debug-Staging 到 Debug 只是为了.NET Standard CreateBuildConfig 项目,让其他项目改用 Debug。
完成后我在构建配置映射中有这个:
- LogicalName 覆盖默认行为并成为嵌入资源的全名。
您的代码假定资源始终嵌入为:
"ConfigBuildConfig.ClientResources.myfolder.config.json"
您可以更改两个 EmbeddedResources,因此 LogicalName 为 config.json,因此无需指定完整路径。
或者,为了避免更改读取嵌入资源的代码,您可以修改 EmbeddedResource 的 LogicalName:
<EmbeddedResource Include="ClientResources\myfolder\config-staging.json" Condition="'$(Configuration)' == 'Debug-Staging'">
<LogicalName>ConfigBuildConfig.ClientResources.myfolder.config.json</LogicalName>
</EmbeddedResource>
一旦这两件事得到解决,data.Environment 显示 'staging' 当 Debug-Staging 被 select 编辑为配置时, 'production' 当 Debug 是 select编辑为主工具栏中的配置。
为了使 Debug-Staging 配置的调试工作,我必须更新 .NET Standard 项目中的配置。 DebugType、DebugSymbols 是两个需要添加的 MSBuild 属性,但完整的更改如下所示:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug-Staging|AnyCPU' ">
<IntermediateOutputPath>obj\Debug-Staging</IntermediateOutputPath>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<OutputPath>bin\Debug-Staging</OutputPath>
<DefineConstants></DefineConstants>
<NoWarn></NoWarn>
<NoStdLib>false</NoStdLib>
</PropertyGroup>
我正在为 Mac 使用 Visual Studio。
我发现了这个 https://forums.xamarin.com/discussion/96777/how-to-set-info-plist-properties-with-vs-build-configurations,它描述了如何使用特定的构建配置设置辅助 plist 文件。效果不错..
<None Include="Entitlements.plist" Condition="'$(Configuration)' != 'Debug-Staging'" />
<None Include="Entitlements-staging.plist" Condition="'$(Configuration)' == 'Debug-Staging'">
<LogicalName>Entitlements.plist</LogicalName>
</None>
但是这种方法不适用于嵌入式资源...
<EmbeddedResource Include="ClientResources\myfolder\config.json"
Condition="'$(Configuration)' != 'Debug-Staging'" />
<EmbeddedResource Include="ClientResources\myfolder\config-staging.json"
Condition="'$(Configuration)' == 'Debug-Staging'">
<LogicalName>config.json</LogicalName>
</EmbeddedResource>
我也试过使用完整路径的 LogicalName... 将条件放在 ItemGroup 上也无济于事。
编辑:
这是我创建的示例项目... https://github.com/JulesMoorhouse/ConfigBuildConfig/blob/master/ConfigBuildConfig/ConfigBuildConfig.csproj
如果选择 Debug-Staging
配置会发生什么,它应该读取 config-staging.json
并在控制台中输出值。
这些消息来源表明这应该有效...
- Same source, multiple targets with different resources (Visual Studio .Net 2008)
- https://social.msdn.microsoft.com/Forums/en-US/aa2d687d-0f6c-4a1c-af95-201f7e091f5c/embed-resource-conditionally-at-compiletime?forum=csharplanguage
这里似乎有两个主要问题。
- 您的 Debug-Staging 配置已映射到调试配置。
这意味着该条件未用于 EmbeddedResource。
如果在Solutionwindow中右击解决方案,然后selectOptions,然后selectBuild - Configurations,Configuration Mappings就可以看到映射了。
如果您在下拉列表中 select Debug-Staging,您可以看到它在构建时映射到调试。
作为这里的快速修复,我删除了 Debug-Staging 配置,re-added 它,确保检查所有解决方案项目的创建配置,然后映射 Debug-Staging 到 Debug 只是为了.NET Standard CreateBuildConfig 项目,让其他项目改用 Debug。
完成后我在构建配置映射中有这个:
- LogicalName 覆盖默认行为并成为嵌入资源的全名。
您的代码假定资源始终嵌入为:
"ConfigBuildConfig.ClientResources.myfolder.config.json"
您可以更改两个 EmbeddedResources,因此 LogicalName 为 config.json,因此无需指定完整路径。
或者,为了避免更改读取嵌入资源的代码,您可以修改 EmbeddedResource 的 LogicalName:
<EmbeddedResource Include="ClientResources\myfolder\config-staging.json" Condition="'$(Configuration)' == 'Debug-Staging'">
<LogicalName>ConfigBuildConfig.ClientResources.myfolder.config.json</LogicalName>
</EmbeddedResource>
一旦这两件事得到解决,data.Environment 显示 'staging' 当 Debug-Staging 被 select 编辑为配置时, 'production' 当 Debug 是 select编辑为主工具栏中的配置。
为了使 Debug-Staging 配置的调试工作,我必须更新 .NET Standard 项目中的配置。 DebugType、DebugSymbols 是两个需要添加的 MSBuild 属性,但完整的更改如下所示:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug-Staging|AnyCPU' ">
<IntermediateOutputPath>obj\Debug-Staging</IntermediateOutputPath>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<OutputPath>bin\Debug-Staging</OutputPath>
<DefineConstants></DefineConstants>
<NoWarn></NoWarn>
<NoStdLib>false</NoStdLib>
</PropertyGroup>