【问题标题】:Compiling c# in linux using mcs compiler gives error : The type or namespace name `Forms' does not exist in the namespace `System.Windows'?使用 mcs 编译器在 linux 中编译 c# 时出现错误:名称空间“System.Windows”中不存在类型或名称空间名称“Forms”?
【发布时间】:2021-03-17 06:05:45
【问题描述】:

我正在尝试在 linux 中编译一些 c# 代码,我可以在 Windows 中使用 Visual Studio 命令行中的 csc 命令编译相同的代码。我也已经安装了mono-devel。

这是确切的错误:

mycode.cs(8,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?

mycode.cs(70,27): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?

为什么会发生这种情况以及如何解决?

这是我在 linux 中用来编译它的命令:

mcs mycode.cs

在代码中,Windows 窗体已经包含在内:

using System.Windows.Forms;

简单的 hello world 代码也可以毫无错误地编译,我基本上遵循了这篇关于如何在 linux 中编译 c# 代码来设置所有内容的博文:

https://jonsson.xyz/2016/11/23/csharp-linux/

【问题讨论】:

  • .net 的哪个版本?我认为直到 .net core 3.1 之后,新的 .net 才引入桌面表单(您可以在线查看)
  • @ADyson 我不确定,我只按照这里的建议在 linux 中安装了 mono-devel:jonsson.xyz/2016/11/23/csharp-linux。代码也是给我的,不是我写的,不过代码没那么复杂,也就300-400行代码吧。
  • @ADyson 也可以编译简单的 hello world 代码而没有任何错误
  • 什么是 Turbo C#? ._.
  • 您关注的博客文章显示了一个简单的控制台应用程序,因此不能证明您可以在那里使用 WinForms。不要浪费时间,坚持使用 Windows。

标签: c# linux compiler-errors mono


【解决方案1】:

你试过用-r:System.Windows.Forms.dll编译吗?

例如:

wc.cs:

using System.Windows.Forms;

public class Program
{
    [STAThread]
    public static void Main()
    {
        var f = new Form();
        f.Text = "Hello World";
        Application.Run(f);
    }
}

然后运行: $ mono-csc wf.cs -r:System.Windows.Forms.dll 你会得到wf.exe

【讨论】:

  • 你能编译我给出的示例程序吗?您可能需要添加更多对 . DLL
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多