Add build errors if nuget package added to projects targeting unsupported .NET TFMs
.NET developers can set TFM to net6.0 and net6.0-windows. If they then add a nuget reference to the WinAppSDK, the build will succeed, but the winappsdk will not be referenced, leading to developer confusion.
A way around this is to add a ResolveAssemblyReferences target that raises an error for netcoreapp as a fallback.
<Project xmlns="[http://schemas.microsoft.com/developer/msbuild/2003">](http://schemas.microsoft.com/developer/msbuild/2003%22%3E)
<Target Name="WindowsAppSDKTFMCheck" BeforeTargets="ResolveAssemblyReferences;Build;Rebuild">
<Error Condition="'$(OutputType)'!='Library'"
Text = "The 'Microsoft.WindowsAppSDK' nuget package cannot be used to target '$(TargetFramework)'. Target 'net6.0-windows10.0.18362.0' or later instead." />
</Target>
</Project>
Thanks to @dotmorten for pointing this technique out. Approach is similar to this PR: https://github.com/dotnet/reactive/pull/1442/files
Additional note:
\build\Microsoft.WindowsAppSDK.props and \build\Microsoft.WindowsAppSDK.targets (as well as buildTransitive\*) needs to be moved to subfolders net6.0-windows10.0.xxxxx and native, so that the app sdk is only getting included into the project types that actually support the windows app sdk. (the rest of the files can be in a shared folder and just referenced with a relative path, which might also help avoiding all the duplication in build and buildTransitive folders).
Instead of putting it in netcoreapp folder, you might want to put the build error in netstandard1.0 so other projects like .NET Framework and .net standard libraries will also get the build error.