简体   繁体   中英

Event handlers C# and TextBlock

I'm new here and with c#. I'm trying to use c# for speech recognizing with a Raspberry using a sample code found on the web. My program crashes when I call TextBlock.Text from a method. Maybe it's because this is an event handler. How can I solve this? If I use these three lines of code in another point of the program (method not used as event handler) it works. Sorry but I'm new and I don't know how this language works, if you can correct the code I would understand it better.

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!
                    });

                };
}

I believe that you need to invoke the thread that the textbox was created in. Therefore, it should be written as:

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

Rather than:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM