1

I want to add buttons from a list, depending of how many items there are in the list. It works perfectly when I do it like this:

enter image description here

The thing is they have no click events, I want each button to have an event that makes the user navigate to the right page depending on which button is clicked.

I'm trying to do it this way but it doesn't work:

enter image description here

Any ideas of the right way to do it if this is totally wrong?

5
  • define "doesn't works" Commented Jun 7, 2017 at 14:32
  • It says "; expected" but I don't think that's the problem. It would be embarrassing if I missed something like that, but I can't find where it should be placed if that's the case. Commented Jun 7, 2017 at 14:34
  • have you tried adding a ";" where the red squiggle is? Commented Jun 7, 2017 at 14:37
  • 1
    Yes I have. The message I get when I think the ";" is correctly placed is "Cannot convert from 'void' to 'Xamarin.Forms.View'". Commented Jun 7, 2017 at 14:39
  • 1
    Firstly, you should really never post images in place of thoughtfully typed out code. Secondly, where that red squiggle is, is definately where you want your ';' we likely need to see more of your code to determine why you're getting that error. Commented Jun 7, 2017 at 14:46

2 Answers 2

3

Create the button before adding in to your StackLayout:

foreach(var item in question.Answers)
{
     var button = new Button();
     button.Text = item.AnswerText;
     button.Clicked += async delegate { await Navigation.PushAsync(item.NextPage); };

     stack.Children.Add(button);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can try with this.

foreach(var item in question.Answers){

    var button = new Button{Text=item.AnswerText};
    button.Clicked += async(s,e)=> await Navigation.PushAsync(item.NextPage);
    stack.Children.Add(button);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.