“ Forms”不存在于名称空间 system.windows 中

我刚刚开始研究 c # ,正在处理从某个论坛得到的一些代码示例。

这段代码使用了一个名称空间 using system.windows.forms,我得到了一个错误:

表单不存在于名称空间 system.windows 中。

此外,我得到了一些错误相关的未定义的函数为 senddownsendup,我相信是在 Forms的名称空间。

我使用的是可视化工作室10(与.net 框架工作4.0)。有什么想法如何修复这个错误?

124840 次浏览

Expand the project in Solution Tree, Right-Click on References, Add Reference, Select System.Windows.Forms on Framework tab.

You need to add reference to some non-default assemblies sometimes.

From comments: for people looking for VS 2019+: Now adding project references is Right-Click on Dependencies in Solution Explorer.

For people looking for VS Code: How do I add assembly references in Visual Studio Code

In case someone runs into this error when trying to reference Windows Forms components in a .NET Core 3+ WPF app (which is actually not uncommon), the solution is to go into the .csproj file (double click it in VS2019) and add it to the property group node containing the target frameworks. Like this:

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

If you are writing Windows Forms code in a .Net Core app, then it's very probable that you run into this error:

Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

If you are using the Sdk style project file (which is recommended) your *.csproj file should be similar to this:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>MyAppNamespace</RootNamespace>
<AssemblyName>MyAppName</AssemblyName>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.0.0" />
</ItemGroup>
</Project>

Pay extra attention to these lines:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.0.0" />

Note that if you are using WPF while referencing some WinForms libraries you should add <UseWPF>true</UseWPF> as well.

Hint: Since .NET 5.0, Microsoft recommends to refer to SDK Microsoft.Net.Sdk in lieu of Microsoft.Net.Sdk.WindowsDesktop.

You may encounter this problem if you have multiple projects inside a solution and one of them is physically located inside solution folder. I solved this by right click on this folder inside Solution tree -> then pressing "exclude from project"

exclude folder

Net >= 5

<TargetFramework>
net5.0-windows
</TargetFramework>

Quoting Announcing .NET 5.0:

Windows desktop APIs (including Windows Forms, WPF, and WinRT) will only be available when targeting net5.0-windows. You can specify an operating system version, like net5.0-windows7 or net5.0-windows10.0.17763.0 ( for Windows October 2018 Update). You need to target a Windows 10 version if you want to use WinRT APIs.

In your project:

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


<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>


</Project>

Also interesting:

  • net5.0 is the new Target Framework Moniker (TFM) for .NET 5.0.
  • net5.0 combines and replaces netcoreapp and netstandard TFMs.
  • net5.0 supports .NET Framework compatibility mode
  • net5.0-windows will be used to expose Windows-specific functionality, including Windows Forms, WPF and WinRT APIs.
  • .NET 6.0 will use the same approach, with net6.0, and will add net6.0-ios and net6.0-android.
  • The OS-specific TFMs can include OS version numbers, like net6.0-ios14.
  • Portable APIs, like ASP.NET Core will be usable with net5.0. The same will be true of Xamarin forms with net6.0.
browxy.com
Compilation failed: 1 error(s), 0 warnings
main.cs(7,24): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?

browxy.com