【问题标题】:InitializeComponent does not exist in the current context, using Mono C# compiler当前上下文中不存在 InitializeComponent,使用 Mono C# 编译器
【发布时间】:2013-01-25 14:37:15
【问题描述】:

我在 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 的案例中,我已经看到了很多关于这个问题的答案;我想知道我应该怎么做才能解决这个问题。谢谢。

【问题讨论】:

    标签: c# linux ubuntu mono initializecomponent


    【解决方案1】:

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

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

    【讨论】:

    • partial关键字来看,我想肯定是从MSVS导入的。
    • @Antonijn:嗯,它看起来可能是-但是您可以轻松地手动编写部分类...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2016-06-01
    • 2018-06-06
    • 1970-01-01
    • 2014-05-16
    相关资源
    最近更新 更多