我有这段代码将语音转换为文本,并以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;
}
}
您知道如何解决此问题吗?