1

我在 Ubuntu 12.04 中使用 mcs 版本 2.10.8.1,我有以下代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName;
                fileName = dlg.FileName;
                MessageBox.Show(fileName);
            }
        }
    }
}

我使用命令编译

$ mcs source_code.cs -r:System.Windows.Forms.dll -r:System.Drawing.dll 

我收到错误

source_code.cs(11,13): error CS0103: The name `InitializeComponent' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings

在使用 Visual Basic 的情况下,我已经看到了很多关于这个问题的答案。我想知道我应该怎么做才能解决这个问题。谢谢。

4

1 回答 1

2

您的 C# 代码最初是在 Visual Studio 中创建的吗?如果是这样,那么您可能会拥有一个Form1.Designer.cs文件以及包含您手工编写的代码的文件。您需要在命令行中包含该文件。

如果这不是最初在 Visual Studo 中创建的 C# 代码,您甚至可能没有InitializeComponent方法……但在这种情况下,您将需要更多代码来在表单中执行任何有用的操作(例如创建按钮并连接其Click事件)。

于 2013-02-10T09:04:42.593 回答