繁体   English   中英

InitializeComponent 在当前上下文中不存在,使用 Mono C# 编译器

[英]InitializeComponent does not exist in the current context, using Mono C# compiler

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM