【问题标题】:Xamarin For Loop won't wait for onActvitityResultXamarin For Loop 不会等待 onActvitityResult
【发布时间】:2016-12-18 19:23:38
【问题描述】:

我正在创建类似于语音识别准确性游戏的东西,并且我正在使用 for 循环,其中包含我需要命名的单词,但问题是在开始活动以获得结果之后它只是继续运行到下一个单词而没有给你有机会回答。我已经看到了有两种方法的可能替代方法,一种是回调类(从未做过这样的事情),并以某种方式让它们相互中继,但我不确定这是如何工作的,也找不到详细的解释。这是我的 for 循环方法

for (int i = 0; i < vocabWords.Length; i++)
        {
            WordToGuess.Text = vocabWords[i];
                CurrWord = vocabWords[i];
            textBox.Text = "";
            string messageSpeakNow = "Speak";
            var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
            voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
            voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, messageSpeakNow);
            voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);
            voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);
            voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);
            voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);
            voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
            StartActivityForResult(voiceIntent, VOICE);
        }

然后我有我的 onactivityresult ,我希望 for 循环等待它,但它只是一遍又一遍地用谷歌麦克风重新弹出,然后在循环完成后停止,永远不会让用户有机会回答
protected override void OnActivityResult(int requestCode, Result resultVal, Intent data) {

            if (requestCode == VOICE)
            {
                if (resultVal == Result.Ok)
                {
                    var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
                    if (matches.Count != 0)
                    {
                        bool saidIt = false;
                        while (!saidIt)
                        {
                            string textInput = textBox.Text + matches[0];
                            textBox.Text = textInput;
                            //set alert for executing the task
                            AlertDialog.Builder alert = new AlertDialog.Builder(this);

                            alert.SetTitle("Is " + textBox.Text + ", what you said?");

                            alert.SetPositiveButton("Yes", (senderAlert, args) =>
                            {
                                noResponse = false;
                                saidIt = true;
                                SetResult(Result.Ok);
                            //change value write your own set of instructions
                            //you can also create an event for the same in xamarin
                            //instead of writing things here
                            if (textBox.Text == CurrWord)
                                {
                                    guess(1);
                                }
                                else
                                {
                                    guess(0);
                                }
                            });

                            alert.SetNegativeButton("No", (senderAlert, args) =>
                            {
                            //perform your own task for this conditional button click
                        });
                            //run the alert in UI thread to display in the screen
                            RunOnUiThread(() =>
                            {
                                alert.Show();
                            });

                        }
                    }
                    base.OnActivityResult(requestCode, resultVal, data);
                }
            }


    }

【问题讨论】:

标签: android for-loop android-intent xamarin


【解决方案1】:

StartActivityForResult 不等待结果,因此您不能在循环中使用它。您必须对第一个单词执行类似调用 StartActivityForResult 之类的操作,然后当您在 OnActivityResult 中获得结果时,查看您是否已完成所有单词(使用索引和 vocabWord 的一些类变量),如果还没有为下一个单词调用 StartActivityForResult,以此类推,直到完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2019-02-28
    • 2020-12-21
    • 2020-10-29
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多