处理后台应用程序的 Visual Studio 目标系统部分将项目中的每个库都视为与后台应用程序使用相同的语言 (C++)。
在这种情况下,托管运行时组件被视为 C++ 组件。因此,.NET 库不包含在部署中。
下一版本的 Visual Studio 应该包含对此的修复,但在那之前,我将它添加到我的后台应用程序的 vcxproj 中:
<!-- Workaround for bug in MSBuild regarding Native Background Applications referencing Managed Conponents -->
<PropertyGroup>
<CopyNuGetImplementations>true</CopyNuGetImplementations>
<NuGetRuntimeIdentifier>win10-$(PlatformTarget.ToLower())</NuGetRuntimeIdentifier>
</PropertyGroup>
<Target Name="_LocalResolvePrimaryProjectWinmdFiles" BeforeTargets="BeforeGenerateAppxManifest" AfterTargets="_ResolvePrimaryProjectWinmdFiles" Condition="'$(OutputType)' != 'exe' and '$(AppxPackage)' == 'true' AND '$(ContainsStartupTask)' == 'true'">
<ItemGroup>
<_AppxWinmdFilesToHarvest Remove="@(_AppxWinmdFilesToHarvest)" />
<_AppxWinmdFilesToHarvest Include="@(PackagingOutputs)" Condition="'%(PackagingOutputs.Extension)' == '.winmd' and '%(PackagingOutputs.ProjectName)' == '$(ProjectName)' and '%(PackagingOutputs.ResolvedFrom)' != 'GetSDKReferenceFiles'">
<!-- This covers the Managed Background Application winmd which does NOT have a WinMDFileType value set -->
<ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == ''">WindowsRuntime 1.4;CLR v4.0.30319</ImageRuntime>
<!-- This covers the C++ Background Application winmd which does NOT have a WinMDFileType value set -->
<ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '@(Language)' == 'C++'">WindowsRuntime 1.4</ImageRuntime>
<!-- This covers Managed Windows Runtime Component winmds -->
<ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '%(PackagingOutputs.WinMDFileType)' == 'Managed'">WindowsRuntime 1.4;CLR v4.0.30319</ImageRuntime>
<!-- This covers Native Windows Runtime Component winmds -->
<ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '%(PackagingOutputs.WinMDFileType)' == 'Native'">WindowsRuntime 1.4</ImageRuntime>
<!-- This covers Native Windows Runtime Component winmds for DynamicLibrary projects -->
<ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' == '' and '%(PackagingOutputs.ProjectType)' == 'DynamicLibrary'">WindowsRuntime 1.4</ImageRuntime>
<!-- This provides an override -->
<ImageRuntime Condition="'$(PrimaryProjectWinmdImageRuntimeOverride)' != ''">$(PrimaryProjectWinmdImageRuntimeOverride)</ImageRuntime>
</_AppxWinmdFilesToHarvest>
</ItemGroup>
</Target>
使用该代码块,.NET 库与后台应用程序一起部署,并且本机代码可以成功访问托管组件。