19

I'm porting a winform app from net core 3.1 to net 5 and getting the following error.

Severity Code Description Project File Line Suppression State Error NETSDK1136 The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so. PublicOutput.core C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 369

This is the section of Microsofts.net.sdk.DefaultItems.targets that this is referring to.

  <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" >

I don't understand the error and the link where the error sends me is not helpful

https://learn.microsoft.com/en-us/visualstudio/?f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(NETSDK1136)%26rd%3Dtrue&view=vs-2019

I've got my target framework set to the following:

    <TargetFramework>net5.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>

any help would be appreciated

ADyson
  • 57,178
  • 14
  • 51
  • 63
M Leipper
  • 277
  • 5
  • 20
  • 1
    "usually by including '-windows' in the TargetFramework property" ...so did you do this? – ADyson Nov 11 '20 at 12:43
  • P.S. https://developercommunity.visualstudio.com/content/problem/1218104/cannot-compile-wpf-applications-targeting-net-5.html - what version of .NET 5 are you running? It mentions WPF but seems a similar issue – ADyson Nov 11 '20 at 12:45
  • 2
    "I've got my target framework set to the following `net5.0`" ..ok so clearly that doesn't include the `-windows` flag, does it? See https://www.thomasclaudiushuber.com/2020/03/26/net-5-merging-net-core-and-net-standard-with-new-target-framework-monikers-tfms/ where it mentions `net5.0-windows` – ADyson Nov 11 '20 at 12:46
  • 1
    Alternatively, https://nicksnettravels.builttoroam.com/net-5-tfms/ might help you – ADyson Nov 11 '20 at 12:47
  • @ADyson I've updated my question to include the target framework. I'm using SDK 5.0.100 as my version of net 5. – M Leipper Nov 11 '20 at 12:47
  • 1
    I already saw that - read my last 2 comments, above. – ADyson Nov 11 '20 at 12:50

3 Answers3

38

The error is clear:

The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF,

so change <TargetFramework>net5.0</TargetFramework> to <TargetFramework>net5.0-windows</TargetFramework> as written in docs

magicandre1981
  • 27,895
  • 5
  • 86
  • 127
0

That may happen if you share a project on a DevOps Env like Azure.

To get the root of that problem, I tried to build every single project one by one and realized that One of my teammates added a new WinForm project into our API project

.csproj file

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);net6.0-windows</TargetFrameworks>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

I was using a Mac so I could not build.

Including '-windows' in the TargetFramework property will not help in this case.

I just unloaded that project from the solution and then I was able to rebuild it with no errors again.

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
-4

Change target framework to right version in project properties.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140