繁体   English   中英

命名空间系统中不存在“表单”。windows

[英]'Forms' does not exist in the namespace system.windows

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

此代码使用命名空间using system.windows.forms我收到错误:

Forms 在命名空间系统中不存在。windows。

此外,我收到了一些与senddownsendup的未定义函数相关的错误,我认为这些错误位于Forms命名空间中。

我正在使用 Visual Studio 10(使用 .net 框架工作 4.0)。 知道如何解决此错误吗?

展开解决方案树中的项目,右键单击ReferencesAdd Reference , Select System.Windows.FormsFramework选项卡上。

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

来自评论:对于寻找 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 .

网 5

引用宣布 .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>

也很有趣:

  • net5.0 是 .NET 5.0 的新目标框架名字对象 (TFM)。
  • net5.0 结合并替换了 netcoreapp 和 netstandard TFM。
  • net5.0 支持 .NET 框架兼容模式
  • net5.0-windows 将用于公开特定于 Windows 的功能,包括 Windows Forms、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 forms 也是如此。

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

排除文件夹

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM