How to use System.Windows.Forms in .NET Core class library

拟墨画扇 提交于 2019-12-28 06:32:08

问题


I've created .NET Core class library and try to build it against net40 framework. I want to use Clipboard class from System.Windows.Forms assembly. How can I do this?

My project.json file:

{
    "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
            }
        }
    }
}

All my net40 specific code is under NET40 define. Any thoughts?


回答1:


What you need is "frameworkAssemblies", for example:

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

Working with Clipboard also requires setting the main thread as STA, so don't forget to add [STAThread] to Main() in your application.




回答2:


Mixing frameworks is certainly one way to go - but then, why do you use .NET Core ?

But what you can do is port the mono implementation of System.Windows.Forms to NetStandard.
Such as here: https://github.com/ststeiger/System.CoreFX.Forms



来源:https://stackoverflow.com/questions/38460253/how-to-use-system-windows-forms-in-net-core-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!