简体   繁体   中英

How can I pass my list to button click event?

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)
  {

  }
}

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; }
    ...
}

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