代码中有1个错误:) [英] 1 error in code :)
本文介绍了代码中有1个错误:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误是:
''string.Trim(params char[])'' is a ''method'', which is not valid in the given context
源代码是:
The Error is:
''string.Trim(params char[])'' is a ''method'', which is not valid in the given context
And The Source Code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.Speech.Recognition;
namespace SpeechToTextTest
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine withEventsField_sre;
public SpeechRecognitionEngine sre
{
get { return withEventsField_sre; }
set
{
if (withEventsField_sre != null)
{
withEventsField_sre.LoadGrammarCompleted -= sre_LoadGrammarCompleted;
withEventsField_sre.SpeechHypothesized -= sre_SpeechHypothesized;
withEventsField_sre.SpeechRecognitionRejected -= sre_SpeechRecognitionRejected;
withEventsField_sre.SpeechRecognized -= sre_SpeechRecognized;
}
withEventsField_sre = value;
if (withEventsField_sre != null)
{
withEventsField_sre.LoadGrammarCompleted += sre_LoadGrammarCompleted;
withEventsField_sre.SpeechHypothesized += sre_SpeechHypothesized;
withEventsField_sre.SpeechRecognitionRejected += sre_SpeechRecognitionRejected;
withEventsField_sre.SpeechRecognized += sre_SpeechRecognized;
}
}
}
public Form1()
{
Load += Form1_Load;
InitializeComponent();
sre = new SpeechRecognitionEngine();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] words = new string[] { "Kathy S" };
Choices c = new Choices(words);
GrammarBuilder grmb = new GrammarBuilder(c);
Grammar grm = new Grammar(grmb);
sre.LoadGrammar(grm);
}
private void btnLiterate_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim.Length == 0)
return;
sre.SetInputToWaveFile(TextBox1.Text);
RecognitionResult r = default(RecognitionResult);
r = sre.Recognize();
if (r == null)
{
TextBox2.Text = "Could not fetch result";
return;
}
TextBox2.Text = r.Text;
}
private void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
OpenFileDialog fileDlg = new OpenFileDialog();
//wave As WaveFile()
if (fileDlg.ShowDialog() == DialogResult.OK)
{
//wave = New WaveFile(fileDlg.FileName)
TextBox1.Text = "Reading .WAV file...";
//wave.Read()
TextBox1.Text = OpenFileDialog1.FileName;
//TextBox1.Text = "Finished Reading .WAV file..."
//m_DrawWave = True
Refresh();
}
}
private void sre_LoadGrammarCompleted(object sender, System.Speech.Recognition.LoadGrammarCompletedEventArgs e)
{
}
private void sre_SpeechHypothesized(object sender, System.Speech.Recognition.SpeechHypothesizedEventArgs e)
{
System.Diagnostics.Debug.Print(e.Result.Text);
}
private void sre_SpeechRecognitionRejected(object sender, System.Speech.Recognition.SpeechRecognitionRejectedEventArgs e)
{
System.Diagnostics.Debug.Print("Rejected: " + e.Result.Text);
}
private void sre_SpeechRecognized(object sender, System.Speech.Recognition.SpeechRecognizedEventArgs e)
{
System.Diagnostics.Debug.Print(e.Result.Text);
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
}
}
推荐答案
而不是以下行:
Instead of the following line :
if (TextBox1.Text.Trim.Length == 0)
您应该写以下行:
You should write the following line:
if (TextBox1.Text.Trim().Length == 0)
因为 Trim()
是String
类的方法,而不是属性:)
Because, Trim()
is a method of the String
class, rather than a property :)
我在代码"Refresh();"之后添加到代码中. />
I added to the code, after the line of "Refresh(); "
DialogResult dr = default(DialogResult);
dr = OpenFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
if (!OpenFileDialog1.FileName.Contains("C:\\Documents and Settings\\Kathy\\Desktop\\SpeechToTextTest\\SpeechToTextTest\\OpenFileDialog1.wav"))
{
MessageBox.Show("Incorrect file");
}
else
{
TextBox1.Text = OpenFileDialog1.FileName;
}
}
并且在出现错误文件"窗口时仍然出现错误消息
and still got error message in appearing the window "Incorrect File"
这篇关于代码中有1个错误:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文