Migration dotnet 6 prompts that the target platform must be set as Windows platform

I'm migrating an old project for NET 6 framework, but VS prompt error NETSDK1136 if you use windows form or WPF, or reference a project or package using windows form or WPF, you must set the target platform to Windows platform. But I don't want this project to be bound to the windows platform, so I don't want to modify it to net6.0 on the TargetFramework 0-windows framework

This prompt is in Net SDK NET. Sdk. DefaultItems. The targets file is opened with the following code

  <Target Name="_CheckForInvalidWindowsDesktopTargetingConfiguration"
        BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
        Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '5.0')) and ('$(UseWindowsForms)' == 'true' or '$(UseWPF)' == 'true')">
    <NETSdkError Condition="'$(TargetPlatformIdentifier)' != 'Windows'"
                 ResourceName="WindowsDesktopTargetPlatformMustBeWindows" />
  </Target>

Or defined in Microsoft NET. Sdk. DefaultItems. Shared. Targets code

  <Target Name="_CheckForTransitiveWindowsDesktopDependencies"
          AfterTargets="ResolvePackageAssets"
          Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '5.0')) and '$(TargetPlatformIdentifier)' != 'Windows' and '@(TransitiveFrameworkReference)' != ''">
    <ItemGroup>
      <_WindowsDesktopTransitiveFrameworkReference Include="@(TransitiveFrameworkReference)"
                                                   Condition="'%(Identity)' == 'Microsoft.WindowsDesktop.App' Or
                                                              '%(Identity)' == 'Microsoft.WindowsDesktop.App.WPF' Or
                                                              '%(Identity)' == 'Microsoft.WindowsDesktop.App.WindowsForms'" />
    </ItemGroup>
    <NetSdkError Condition="'@(_WindowsDesktopTransitiveFrameworkReference)' != ''"
                 ResourceName="WindowsDesktopTargetPlatformMustBeWindows" />
  </Target>

That is to say, in NET 5.0 and above. If you judge that Windows Forms or WPF is used, you will be prompted to add windows platform

The judgment here is to include the project itself and all projects and libraries that the project depends on. As long as one is used, it will prompt that the Windows platform needs to be added

Adding the windows platform to the settings naturally refers to the load of Windows Forms or WPF. My project does not expect to be bound to windows, so I need to find out which dependent projects or libraries use Windows Forms or WPF projects

It must be noted here that WPF and Windows Forms can run in Linux and MAC environments with limited support when they do nothing, including MONO. What is limited support? As long as you don't touch the logic of Windows related platforms, including PInvoke calls, it will run well

For example, only some structure definitions of WPF are used, such as Rect and Size. Or enumeration definitions are used, or some tools and methods are used, etc

My current project happens to have some dependent libraries. WPF is referenced just to use the definitions such as Size. In order to kill dependence, I replaced it with https://github.com/dotnet-campus/dotnetCampus.WPFType Open source libraries instead of referencing WPF projects. This open source library copies some type definitions of WPF and is based on MIT protocol

However, after gradually eliminating all dependencies, the following tips are still given

C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets(250,5): error NETSDK1136: If used Windows Form or WPF,Or use by reference Windows Form or WPF The target platform must be set to Windows (Usually through TargetFramework Add to property "-windows"). 

In fact, the reason is that there is a cache in the obj folder. You only need to delete the obj folder and rebuild it

Keywords: WPF

Added by renaker on Wed, 26 Jan 2022 07:19:02 +0200