c# - "' 表格1 ' already defines a member called ' .ctor ' with the same parameter types "

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

我有这段代码,它将语音转换为文本并将其打印在 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;
    }
}

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

最佳答案

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

关于c# - "' 表格1 ' already defines a member called ' .ctor ' with the same parameter types ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57927754/

相关文章:

c# - 没有属性名称的Http get和post方法

c# - 来自 MemberExpression 的反射类型

c# - 如何在Richtextbox的每一行的开头添加一个字符

C# WinForms - 图片/图像框选择 - 类似于 MS Paint

c# - 禁用报告按钮链接不想禁用 - html5

c# - 如何以编程方式在 Windows 服务应用程序中创建暂停?

c# - 如何在保持趋势的同时删除常规间隔中的 n% 的列表?

c# - 如何监控特定应用程序的网络带宽使用情况?

.net - 为什么我与 System.Data.DataRow 对象中包含的值的 null 比较失败?

c# - 清除类型为 : DropDownList 的 Winform Combobox 中的默认空值