I am using ListView from Xamarin.Forms for my project.
I have a List<MyObject>
, where each object MyObject has an individual URL property.
The List is used as the ItemsSource for the ListView.
How can I let each list item open the URL it has been assigned to, when clicked on?
Asked
Active
Viewed 98 times
-3

taratect
- 3
- 1
-
Have you tried using reflexion on your list? Or maybe a simple foreach-loop? – Adrian Efford Jun 15 '20 at 10:07
-
@AdrianEfford What do you mean with reflexion? I didn't find anything about it on google. – taratect Jun 15 '20 at 10:17
-
Reflection sorry... Here a link: https://stackoverflow.com/questions/15234236/reflection-on-list-and-printing-values – Adrian Efford Jun 15 '20 at 10:19
-
You are looking for OnClickEvent for list item, here is how you do it https://www.codemahal.com/video/adding-click-events-to-a-listview-in-android-with-xamarin/ – Ali Jun 15 '20 at 10:28
-
@Ali That's for Xamarin Native, I need Xamarin Forms. – taratect Jun 15 '20 at 11:55
-
See the answer from Jason, but native is really easy to convert to Forms :) – Ali Jun 16 '20 at 02:37
1 Answers
0
use the ItemSelected
event
protected void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs args)
{
var item = (MyObject)args.SelectedItem;
var url = item.Url;
Device.OpenUri(url);
}

Jason
- 86,222
- 15
- 131
- 146