[英]The type or namespace name 'Forms' does not exist in the namespace 'System.Windows'
[英]'Forms' does not exist in the namespace system.windows
我刚刚开始研究 c#,并且正在摆弄我从某个论坛获得的一些代码示例。
此代码使用命名空间using system.windows.forms
我收到错误:
Forms 在命名空间系统中不存在。windows。
此外,我收到了一些与senddown
和sendup
的未定义函数相关的错误,我认为这些错误位于Forms
命名空间中。
我正在使用 Visual Studio 10(使用 .net 框架工作 4.0)。 知道如何解决此错误吗?
展开解决方案树中的项目,右键单击References
, Add Reference
, Select System.Windows.Forms
在Framework
选项卡上。
您有时需要添加对一些非默认程序集的引用。
来自评论:对于寻找 VS 2019+ 的人:现在添加项目引用是在Solution Explorer
中右键单击Dependencies
项。
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). 解决方法是将 go 放入 .csproj 文件中(在 VS2019 中双击它)并将其添加到包含目标框架的属性组节点中。 像这样:
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
如果您在.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>
。
Hint: Since .NET 5.0 , Microsoft recommends to refer to SDK Microsoft.Net.Sdk
in lieu of Microsoft.Net.Sdk.WindowsDesktop
.
引用宣布 .NET 5.0 :
Windows desktop APIs (including Windows Forms, WPF, and WinRT) will only be available when targeting net5.0-windows . 您可以指定操作系统版本,例如 net5.0-windows7 或 net5.0-windows10.0.17763.0(适用于 Windows 2018 年 10 月更新)。 如果要使用 WinRT API,则需要以 Windows 10 版本为目标。
在您的项目中:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
也很有趣:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.