2014-12-07 261 views
0

我在表單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的第一個參數可以找到「

回答

1

你需要讓你的靜態字段或ChatForm類型,而不是Form財產ChatForm

private ChatForm ChatForm; 
+0

THIS IS!謝謝!! – Zenth 2014-12-07 19:33:47

0

richtextBox1的修飾符更改爲在您的窗體中公開。

Changing modifier

如果General.ChatForm是Form類型,使用(General.ChatForm as ChatForm).richtextBox1或作爲達米爾所述改變其類型ChatForm