Creating buttons in a for loop

lhtyex123 21 Reputation points
2020-11-23T11:11:57.863+00:00

Hi all,

I'm new to Xamarin, so sorry if the question is a repeat or explained badly.

What I'm aiming to do is to create and display a number of buttons for each keyphrase found in a phrase and then have the selected keyword spoken back out of the device once the button is clicked. I'm having issues with trying to create buttons and display them within a for loop.
At current in my MainPage XAML I have two frames which display the sentence and one which I would hope will hold the buttons.

What I hoped would have created the buttons and displaye them (in main page code behind):

if (recognizer == null)
            {
                var config = SpeechConfig.FromSubscription(Constants.CognitiveServicesApiKey, Constants.CognitiveServicesRegion);
                var audioConfig = Microsoft.CognitiveServices.Speech.Audio.AudioConfig.FromDefaultMicrophoneInput();
                recognizer = new SpeechRecognizer(config);
                var recognizerkeyword = new KeywordRecognizer(audioConfig);
                recognizer.Recognized += (obj, args) =>
                { 

                    var response = client.ExtractKeyPhrases(args.Result.Text);
                    // pulls out the keywords from the sentence



                    foreach (string keyphrase in response.Value)
                    {

                        newButtonCreation(keyphrase);

                    }



                };

 void newButtonCreation(string keyphrase)

        {

            Device.BeginInvokeOnMainThread(() =>
            {
                if (!string.IsNullOrWhiteSpace(keyphrase))
                {
                    Xamarin.Forms.Button b = new Xamarin.Forms.Button() { HorizontalOptions = LayoutOptions.Fill, VerticalOptions = LayoutOptions.Fill, Text = keyphrase};
                    b.Clicked += async (sender, args) => await TextToSpeech.SpeakAsync(keyphrase);

                }
            }

            );

        }

This doesn't really do anything at the minute and I wonder if I need to edit my mainpage XAML form to display these newly created buttons, but I am unsure of how to do that.

TIA

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,287 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ahmad 81 Reputation points
    2020-11-23T12:09:25.573+00:00

    I noticed two things in your code sample:

    1- You are using a Frame for your buttons, but a Frame layout can only have one child. Use
    StackLayout or any other Layout that can hold more than one child.

    2- You are creating some buttons but never add them to your layout.

    Here is a code sample:

    void newButtonCreation(string keyphrase)
    
             {
    
                 Device.BeginInvokeOnMainThread(() =>
                 {
                     if (!string.IsNullOrWhiteSpace(keyphrase))
                     {
                         Xamarin.Forms.Button b = new Xamarin.Forms.Button
                           { 
                               HorizontalOptions = LayoutOptions.Fill, VerticalOptions = 
                               LayoutOptions.Fill, Text = keyphrase
                           };
    
                         b.Clicked += async (sender, args) => await TextToSpeech.SpeakAsync(keyphrase);
    
                        MyLayout.Children.Add(b);                        
                     }
                 }
    
                 );
             }
    

    Where (MyLayout) is a StackLayout defined in XAML with (x:Name="MyLayout")

    Let me know if that works for you. Best luck.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. lhtyex123 21 Reputation points
    2020-11-23T12:58:14.197+00:00

    @Ahmad Thank you so much, this has massively helped me!!