【问题标题】:How to use System.Windows.Forms in .NET Core class library如何在 .NET Core 类库中使用 System.Windows.Forms
【发布时间】:2022-01-17 05:13:29
【问题描述】:

我创建了 .NET Core 类库并尝试针对 net40 框架构建它。我想使用 System.Windows.Forms 程序集中的剪贴板类。我该怎么做?

我的 project.json 文件:

{
    "version": "1.0.0-*",

    "dependencies": {
        "NETStandard.Library": "1.6.0"
    },

    "frameworks": {
        "netstandard1.6": {
            "imports": "dnxcore50",
            "buildOptions": {
                "define": [
                    "NETCORE"
                ]
            },
            "dependencies": {
                "System.Threading": "4.0.11",
                "System.Threading.Thread": "4.0.0",
                "System.Threading.Tasks":  "4.0.11"
                }
        },
        "net40": {
            "buildOptions": {
                "define": [
                    "NET40"
                    ]
                },
            "dependencies": {
                // dependency should be here but there is no such dll
            }
        }
    }
}

我所有的 net40 特定代码都在 NET40 定义下。有什么想法吗?

【问题讨论】:

  • 是的,你是对的。但您也可以使用 .NET Core 创建简单的控制台应用程序。我尝试使用复制/粘贴功能创建一个自定义控制台以满足我的需求,因此我需要从剪贴板复制数据并将其粘贴到我的控制台。
  • 因此,您必须找到另一个属于 .NET Core 的类才能使用剪贴板或切换到以“Legacy .Net”为目标的控制台应用程序。
  • @MarcoGuignard 这正是net40 框架的用途。它允许您编写可在 .Net Core 和 .Net Framework 上运行的库和应用程序,同时尽可能利用 .Net Framework 特定的功能。

标签: .net .net-core project.json


【解决方案1】:

对于 VS2019 .NET Core 3.1:

  1. 鼠标右键单击项目并选择卸载项目
  2. 鼠标右键单击项目并选择“Edit foobar.csproj”
  3. 在 .NET Core 3.1 中使用 WPF 和 Winforms 的示例:我在其中添加了 UseWPF 和 UseWindowsForms 标签。此外,我将 Microsoft.NET.Sdk 更改为 Microsoft.NET.Sdk.WindowsDesktop 以便也可以使用 wpf。
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
...
  1. 保存并再次右键单击项目并选择重新加载项目

【讨论】:

  • 谢谢。在没有 WPF 的情况下使用 Windows 窗体还需要我设置 &lt;Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"&gt;
  • 我需要 Windows 目标平台来完成这项工作,例如&lt;TargetFramework&gt;net6.0-windows&lt;/TargetFramework&gt;。否则构建会出现错误"The target platform must be set to windows"
【解决方案2】:

你需要的是"frameworkAssemblies",例如:

"frameworks": {
  "netstandard1.6": {
    "dependencies": {
      "NETStandard.Library": "1.6.0"
    }
  },
  "net40": {
    "frameworkAssemblies": {
      "System.Windows.Forms": {}
    }
  }
}

使用Clipboard 还需要将主线程设置为STA,所以不要忘记在您的应用程序中将[STAThread] 添加到Main()

【讨论】:

  • 如何在新的配置文件格式 .csproj 中做同样的事情?
  • @ZEE 你知道 .csproj 中有什么例子吗?
  • @ZEE 看看这个答案link
【解决方案3】:

对于 .Net 6,.csproj 文件应包含:

<PropertyGroup>
     <TargetFramework>net6.0-windows</TargetFramework>
     <UseWPF>true</UseWPF>
     <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

注意目标框架中的“-windows”。

UseWPF 是可选的。

【讨论】:

    【解决方案4】:

    注意:以下内容适用于 .NET Core

    但是,如果您需要在 Linux 上使用 WinForms 编译某些东西,它仍然有效,因为 .NET Core WinForms 仅在 Windows 上运行。

    混合框架当然是一种方法 - 但是,为什么要使用 .NET Core 呢?

    但您可以做的是将 System.Windows.Forms 的单声道实现移植到 NetStandard。
    比如这里: https://github.com/ststeiger/System.CoreFX.Forms

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-11
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2020-05-02
      • 1970-01-01
      相关资源
      最近更新 更多