静态类中的静态事件访问表单控件
问题描述:
我在表单Proyect中使用语音识别,我有一个静态类与函数初始化SR引擎。静态类中的静态事件访问表单控件
在这种静态类,我宣布公共静态形式=新formX()
我的问题是,当我检测到语音识别的事件,我需要更新的formX文本控件,但IDE说文控制不存在的形式,我认为是因为speechEngine使用单独的线程。
static General()
{
General.ChatForm = new ChatForm();
}
public static void startSpeechRecognition()
{
// Setup grammar rules:
GrammarBuilder builder = new GrammarBuilder();
builder.AppendDictation();
grammar = new Grammar(builder);
// Initiate Recognizer and Setup Events:
recognizer = new SpeechRecognitionEngine(/*new CultureInfo("es-ES")*/);
recognizer.LoadGrammar(grammar); // Poner otro try aqui, si falla, es que no tiene configurado el sistema de voz
recognizer.SetInputToDefaultAudioDevice(); // Poner un try aqui, si falla es que no tiene microfono configurado.
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Initialize Recognizer thread:
RecognizerState = false;
RecThread = new Thread(new ThreadStart(RecThreadFunction));
RecThread.Start();
}
static void RecThreadFunction()
{
// this function is on separate thread (RecThread). This will loop the recognizer receive call.
while (true)
{
try
{
recognizer.Recognize();
}
catch
{
// handle Errors. Most errors are caused from the recognizer not recognizing speech.
}
}
}
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
// Event raised when the speech recognizer recognizes speech.
/*
if (!RecognizerState)
{
return;
}
* */
General.ChatForm.richTextBox1.Text += (" " + e.Result.Text.ToLower());
}
我poblem是线General.ChatForm.richTextBox1.Text + =(” “+ e.Result.Text.ToLower());中,IDE节目” System.Windows.Forms.Form中不包含'richtextBox1'的定义,并且没有扩展方法'richTextBox1'接受类型System.Windows.Forms.Form的第一个参数可以找到“
答
你需要让你的静态字段或ChatForm
类型,而不是Form
财产ChatForm
:
private ChatForm ChatForm;
答
将richtextBox1
的修饰符更改为在您的窗体中公开。
如果General.ChatForm是Form
类型,使用(General.ChatForm as ChatForm).richtextBox1
或作为达米尔所述改变其类型ChatForm
。
THIS IS!谢谢!! – Zenth 2014-12-07 19:33:47