【问题标题】:"'Form1' already defines a member called '.ctor' with the same parameter types ""'Form1' 已经定义了一个名为 '.ctor' 的成员,具有相同的参数类型"
【发布时间】:2020-01-15 13:45:53
【问题描述】:

我有这段代码可以将语音转换为文本并以 Windows 形式在列表框上打印,但是当我尝试在 Visual Studio 2017 中编译它时,由于错误“‘Form1’已经定义了一个名为‘.ctor’的成员,它不会编译' 具有相同的参数类型” 。

这是逻辑:

Program.cs

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Speech.Recognition;
using System.Globalization;
namespace WinFormSpeech
{
    public partial class Form1 : Form
    {
        static CultureInfo ci = new CultureInfo("en-IN");
        static SpeechRecognitionEngine sre =
          new SpeechRecognitionEngine(ci);
        public Form1()
        {

            InitializeComponent();
            sre.SetInputToDefaultAudioDevice();
            sre.SpeechRecognized += sre_SpeechRecognized;
            Grammar g_HelloGoodbye = GetHelloGoodbyeGrammar();
            Grammar g_SetTextBox = GetTextBox1TextGrammar();
            sre.LoadGrammarAsync(g_HelloGoodbye);
            sre.LoadGrammarAsync(g_SetTextBox);
            // sre.RecognizeAsync() is in CheckBox event
        }
        static Grammar GetHelloGoodbyeGrammar()
        {
            Choices ch_HelloGoodbye = new Choices();
            ch_HelloGoodbye.Add("hello");
            ch_HelloGoodbye.Add("goodbye");
            GrammarBuilder gb_result =
              new GrammarBuilder(ch_HelloGoodbye);
            Grammar g_result = new Grammar(gb_result);
            return g_result;
        }
        static Grammar GetTextBox1TextGrammar()
        {
            Choices ch_Colors = new Choices();
            ch_Colors.Add(new string[] { "red", "white", "blue" });
            GrammarBuilder gb_result = new GrammarBuilder();
            gb_result.Append("set text box 1");
            gb_result.Append(ch_Colors);
            Grammar gi_result = new Grammar(gb_result);
            return gi_result;
        }
        private void checkBox1_CheckedChanged(object sender,
          EventArgs e)
        {
            if (checkBox1.Checked == true)
                sre.RecognizeAsync(RecognizeMode.Multiple);
            else if (checkBox1.Checked == false) // Turn off
                sre.RecognizeAsyncCancel();
        }
        void sre_SpeechRecognized(object sender,
          SpeechRecognizedEventArgs e)
        {
            string txt = e.Result.Text;
            float conf = e.Result.Confidence;
            if (conf < 0.65) return;
            this.Invoke(new MethodInvoker(() =>
            {
                listBox1.Items.Add("I heard you say: "
              + txt);
            })); // WinForm specific
            if (txt.IndexOf("text") >= 0 && txt.IndexOf("box") >=
              0 && txt.IndexOf("1") >= 0)
            {
                string[] words = txt.Split(' ');
                this.Invoke(new MethodInvoker(() =>
                { textBox1.Text = words[4]; })); // WinForm specific
            }
        }
    } // Form
} // ns

这是设计器代码:

form1.designer.cs

namespace WinFormSpeech
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(672, 42);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(101, 21);
            this.checkBox1.TabIndex = 0;
            this.checkBox1.Text = "Speech On";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(21, 42);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 22);
            this.textBox1.TabIndex = 1;
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 16;
            this.listBox1.Location = new System.Drawing.Point(-1, 97);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(771, 324);
            this.listBox1.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.checkBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.ListBox listBox1;
    }
}

你知道如何解决这个问题吗?

【问题讨论】:

  • .ctor()表示构造函数,public Form1() {...}。但这只是一旦定义,我可以看到。您是否尝试清理解决方案,重新启动VS和REBUILD? span>
  • CTOR应该是“构造函数”的图像。但是如果你没有这个部分类的第三部分,我看不出你可以在哪里重复构造函数。
  • 您确定没有其他带有partial class Form1 的文件带有无参数构造函数吗?

标签: c# .net winforms visual-studio-2017 speech-to-text


【解决方案1】:

尝试将您的类重命名为 Form1 以外的名称。如果在同一个文件夹或命名空间中有多个同名的类,有时 Visual Studio 可能会感到困惑。

【讨论】:

    【解决方案2】:

    我在 DevSkiller 评估中遇到了这个问题。在本地 VS(和 dotnet CLI)中构建良好,但是当我压缩并上传它时,DevSkiller 编译器(每个输出的 .NET Core 版本相同)会给我这些错误,如果有重复的类,你会期望这些错误。

    我记得我修复了其中一个文件夹名称中的拼写错误(“服务”改为“服务”),但 DevSkiller 中的 dotnet 构建 CLI 输出在“服务/...”中显示了这些错误,好像我没有重命名文件夹。命名空间本身从一开始就拼写正确,所以.. 如果我不知道更好,我会说 DevSkiller 出于某种原因正在用自己的副本替换任何丢失的代码,也许是为了解决丢失非代码文件的问题阻止构建其他有效的解决方案?

    为了测试这个理论,我尝试上传这个 DevSkiller 解决方案,其中只包含 sln 文件、“重要”文件,当然还有 devskiller.marker 文件。对于甚至不应该存在的文件,仍然给我同样的构建错误......

    我重命名文件夹可能会导致这个特定的 DevSkiller“会话”无法挽回。伙计们,我不认为我能得到这份工作!

    【讨论】:

      【解决方案3】:

      Form1 类的类文件中已经定义了一个构造函数,即 Form1.cs。您可以通过进一步展开 Form1.Designer.cs 文件来访问此文件。 因此,从这里开始,您可以在构造函数中添加代码,即 Form1() 到 Form1.cs 文件中,而不是在 Form1.Designer.cs 文件中创建另一个 Form1()

      【讨论】:

        【解决方案4】:

        我在一分钟前将它推送到 Github 并使用 Github Action 构建时遇到了这个问题。该项目可以使用 Visual Studio 和dotnet build 很好地构建。但是当我推送到 Github 时,它只是告诉我喜欢你的问题。所以从here,我发现我只需要点击函数名,然后点击rename,然后点击Apply,问题就消失了!

        如果您不相信,请查看this (before)this (after)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-20
          • 1970-01-01
          • 1970-01-01
          • 2020-06-17
          • 2022-12-11
          相关资源
          最近更新 更多