Home > OS >  How can I pass my list to button click event?
How can I pass my list to button click event?

Time:03-27

I was trying to pass my fruit1 list to fruitBtn_Clicked but I was not able to do it. Here is what I tryed before;

fruitBtn_Clicked = (sender, EventArgs) => { fruitBtn_Clicked(sender, EventArgs, fruit1); };

and I got this error;

Severity Code Description Project File Line Suppression State Error XFC0002 EventHandler "fruitBtn_Clicked" with correct signature not found in type "OOP_proje.Views.Test".

Also I tryed this and I got same error;

fruitBtn_Clicked = delegate (object sender2, EventArgs e2) { fruitBtn_Clicked(sender2, e2, fruit1); };

public partial class Test : ContentPage
{
  public Test()
  {
    var fruit1 = new List<Fruits> { };

  }

  private void fruitBtn_Clicked(object sender, EventArgs e)
  {

  }
}

CodePudding user response:

You can't change the delegate's arguments (number or whatever), that's why you get this error.

Just declare your variable 'fruit1' in a higher position to be seen by the delegate:

public partial class Test : ContentPage
{
    var fruit1;
    public Test()
    {
        fruit1 = new List<Fruits> { };

    }

    private void fruitBtn_Clicked(object sender, EventArgs e)
    {
        // use fruit1 here
    }
}

You may also change it to a property (then use a Capital):

public partial class Test : ContentPage
{
    var Fruit1 { get; set; }
    ...
}
  • Related