25

我创建了 .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 定义下。有什么想法吗?

4

4 回答 4

50

对于 VS2019 .NET Core 3.1:

  1. 鼠标右键单击项目并选择卸载项目
  2. 鼠标右键单击项目并选择“编辑 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. 保存并再次右键单击项目并选择重新加载项目
于 2020-03-10T16:01:10.807 回答
19

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

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

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

于 2016-07-20T01:29:15.960 回答
5

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

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

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

UseWPF是可选的。

于 2021-11-29T08:49:26.607 回答
1

注意:波纹管适用于 .NET Core < 3,它在 Windows 上没有 WinForms。

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

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

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

于 2018-08-28T07:00:07.633 回答