簡體   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