I am adding some pins to a map, and when the user tap on this pin (actually the content of the pin) I want to open a specific page.
I want to do something like this:
async void OnPinClicked(Places place)
{
await Navigation.PushAsync(new MyPage(place));
}
private void PopulateMap(List<Places> places)
{
for (int index = 0; index < places.Count; index++)
{
var pin = new Pin
{
Type = PinType.Place,
Position = new Position(places[index].Lat, places[index].Lon),
Label = places[index].Name,
Address = places[index].Address
};
pin.Clicked += (sender, ea) =>
{
Debug.WriteLine("Name: {0}", places[index].Name); // The app is crashing here (if I tap on a pin)
OnPinClicked(places[index]);
};
MyMap.Pins.Add(pin);
}
}
But I don't know if it is possible to pass parameters to the OnPinClicked
function. Is that possible? If it is not, what can I do to solve this?
Note: I'm newbie on Xamarin and C#.
You can't pass arguments to event handlers. You can assign handler by another way: var newIndex = index; // for avoiding closure pin. Clicked += async (s, e) => { await Navigation.
BindingContext
<Button Text="Button1" Clicked="Button1_Clicked" BindingContext="333"/>
string data = ((Button)sender).BindingContext as string;
// data = 333;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With