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

问题描述 投票:0回答:9

我刚刚开始研究 c#,并且正在摆弄从某个论坛获得的一些代码示例。

这段代码使用了一个命名空间

using system.windows.forms
,我收到了一个错误:

命名空间 system.windows 中不存在 Forms。

我还收到一些与

senddown
sendup
的未定义函数相关的错误,我相信它们位于
Forms
命名空间中。

我正在使用 Visual Studio 10(带有 .net Framework 4.0)。知道如何修复这个错误吗?

c# winforms
9个回答
150
投票

在解决方案树中展开项目,右键单击

References
Add Reference
,选择
System.Windows.Forms
选项卡上的
Framework

有时需要添加对一些非默认程序集的引用。

来自评论:对于寻找 VS 2019+ 的人:现在添加项目引用是在 Dependencies 中的

Solution Explorer
右键单击

对于寻找 VS Code 的人:如何在 Visual Studio Code 中添加程序集引用


84
投票

如果有人在尝试引用 .NET Core 3+ WPF 应用程序中的 Windows Forms 组件时遇到此错误(实际上并不罕见),解决方案是进入

.csproj
文件(在中双击它) VS2019)并将其添加到包含目标框架的属性组节点。像这样:

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

20
投票

如果您在 .Net Core 应用程序中编写 Windows Forms 代码,那么您很可能会遇到此错误:

错误 CS0234 命名空间“System.Windows”中不存在类型或命名空间名称“Forms”(是否缺少程序集引用?)

如果您使用 Sdk 样式项目文件(推荐),您的 *.csproj 文件应与此类似:

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

特别注意这些行:

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

请注意,如果您在引用某些 WinForms 库时使用 WPF,则还应该添加

<UseWPF>true</UseWPF>

提示:自 .NET 5.0 起,Microsoft 建议参考 SDK

Microsoft.Net.Sdk
代替
Microsoft.Net.Sdk.WindowsDesktop


14
投票

净值 >= 5

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

引用宣布.NET 5.0

Windows 桌面 API(包括 Windows Forms、WPF 和 WinRT)将仅在面向 net5.0-windows 时可用。您可以指定操作系统版本,例如 net5.0-windows7 或 net5.0-windows10.0.17763.0(适用于 Windows October 2018 Update)。如果您想使用 WinRT API,您需要定位 Windows 10 版本。

在您的项目中:

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

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

</Project>

也很有趣:

  • net5.0 是 .NET 5.0 的新目标框架名称 (TFM)。
  • net5.0 结合并取代了 netcoreapp 和 netstandard TFM。
  • net5.0支持.NET Framework兼容模式
  • net5.0-windows 将用于公开 Windows 特定的功能,包括 Windows 窗体、WPF 和 WinRT API。
  • .NET 6.0 将使用与 net6.0 相同的方法,并将添加 net6.0-ios 和 net6.0-android。
  • 特定于操作系统的 TFM 可以包含操作系统版本号,例如 net6.0-ios14。
  • 可移植 API,如 ASP.NET Core 将可与 net5.0 一起使用。使用 net6.0 的 Xamarin 表单也是如此。

2
投票

如果解决方案中有多个项目,并且其中一个项目实际位于解决方案文件夹内,则可能会遇到此问题。 我通过右键单击解决方案树中的此文件夹解决了这个问题 - >然后按“从项目中排除”


0
投票

对于某些人只需要 Windowsform 命名空间中的结构和一些函数,您只需将项目更改为旧的行为即可。

将DisableWinExeOutputInference设置为true,然后OutputType不会被Visual Studio覆盖:D。

不要忘记将 add -windows 设置为 TargetFramework

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<UseWindowsForms>true</UseWindowsForms>
<StartupObject></StartupObject>
<ApplicationIcon />
</PropertyGroup>

在这里我找到了它们 https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/sdk-and-target-framework-change


0
投票

转到项目属性->应用程序->常规。 选择“为此项目启用 Windows 窗体”复选框。


-1
投票
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?


-1
投票

最干净的解决方案是添加这个 nuget 包 链接到 nuget 包页面

© www.soinside.com 2019 - 2024. All rights reserved.