2016-02-26 1 views
0

Je suis nouveau ici et avec C#. J'essaie d'utiliser C# pour reconnaître la parole avec un Raspberry en utilisant un exemple de code trouvé sur le web. Mon programme se bloque lorsque j'appelle TextBlock.Text à partir d'une méthode. Peut-être que c'est parce que c'est un gestionnaire d'événements. Comment puis-je resoudre ceci? Si j'utilise ces trois lignes de code dans un autre point du programme (méthode non utilisée comme gestionnaire d'événements) cela fonctionne. Désolé mais je suis nouveau et je ne sais pas comment cette langue fonctionne, si vous pouvez corriger le code, je le comprendrais mieux.Gestionnaires d'événements C# et TextBlock

namespace RPiVoice 
{ 

    public sealed partial class MainPage : Page 
    { 
     private const int RED_LED_PIN = 5; 
     ... 

     public MainPage() 
     { 
      this.InitializeComponent(); 
      Unloaded += MainPage_Unload 
      initializeSpeechRecognizer(); 
      initializeGPIO(); 
     } 

     private void initializeGPIO() 
     { 
         gpio = GpioController.GetDefault(); 

      // // Initialize GPIO Pins 
      redPin = gpio.OpenPin(RED_LED_PIN); 
      greenPin = gpio.OpenPin(GREEN_LED_PIN); 
      bedroomLightPin = gpio.OpenPin(BEDROOM_LIGHT_PIN); 

      redPin.SetDriveMode(GpioPinDriveMode.Output); 
      greenPin.SetDriveMode(GpioPinDriveMode.Output); 
      bedroomLightPin.SetDriveMode(GpioPinDriveMode.Output); 

      // Write low initially, this step is not needed 
      redPin.Write(GpioPinValue.Low); 
      greenPin.Write(GpioPinValue.Low); 
      bedroomLightPin.Write(GpioPinValue.Low); 
     } 
     // Initialize Speech Recognizer and start async recognition 
     private async void initializeSpeechRecognizer() 
     { 

      // Initialize recognizer 
      var recognizer = new SpeechRecognizer(new Windows.Globalization.Language("it-IT")); 

      // Set event handlers -> the problem should be here 
      recognizer.StateChanged += RecognizerStateChanged; 
      recognizer.ContinuousRecognitionSession.ResultGenerated += RecognizerResultGenerated; 

     } 

     // Recognizer generated results 
     private async void RecognizerResultGenerated(SpeechContinuousRecognitionSession session, SpeechContinuousRecognitionResultGeneratedEventArgs args) 
     { 
      ... 


        await Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { 
         this.Stato.Text = "test"; //this make the program crash! 
        }); 

       }; 
} 
+0

Vous obtenez un exception, vous pouvez nous montrer? – Gabe

Répondre

0

Je crois que vous avez besoin d'invoquer le fil que la zone de texte a été créé en conséquence, il doit être écrit.

this.Dispatcher.Invoke((Action)(() => 
{ 
     this.Stato.Text = "test"; 
})); 

Plutôt que:

   await Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { 
        this.Stato.Text = "test"; //this make the program crash! 
       });