There are two ways to solve this, and I lean toward the second. Close to what Edward L. said
DisplayAlert is the method on the Xamarin.Forms page ... and you are inside the static method returning this page, so you have no link to it.
So you can do this:
using System; using Xamarin.Forms; namespace DataBinding_Lists { public class App { private static Page page; public static Page GetMainPage () { var listView = new ListView { RowHeight = 40 }; listView.ItemsSource = new Person [] { new Person { FirstName = "Abe", LastName = "Lincoln" }, new Person { FirstName = "Groucho", LastName = "Marks" }, new Person { FirstName = "Carl", LastName = "Marks" }, }; listView.ItemTemplate = new DataTemplate(typeof(TextCell)); listView.ItemTemplate.SetBinding(TextCell.TextProperty, "FirstName"); listView.ItemSelected += async (sender, e) => { await page.DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", ""); }; page = new ContentPage { Content = new StackLayout { Padding = new Thickness (5,20,5,5), Spacing = 10, Children = { listView } } }; return page; } } }
What you really have to do is create a new class that is your page.
Your App.cs turns into this:
using System; using Xamarin.Forms; namespace DataBinding_Lists { public class App { public static Page GetMainPage () { return new PeoplePage(); } } }
Then you create a new class that inherits from the page: using the system; using Xamarin.Forms;
namespace DataBinding_Lists { public class PeoplePage : Page { public PeoplePage() { var listView = new ListView { RowHeight = 40 }; listView.ItemsSource = new Person [] { new Person { FirstName = "Abe", LastName = "Lincoln" }, new Person { FirstName = "Groucho", LastName = "Marks" }, new Person { FirstName = "Carl", LastName = "Marks" }, }; listView.ItemTemplate = new DataTemplate(typeof(TextCell)); listView.ItemTemplate.SetBinding(TextCell.TextProperty, "FirstName"); listView.ItemSelected += async (sender, e) => { await DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", ""); }; Content = new ContentPage { Content = new StackLayout { Padding = new Thickness (5,20,5,5), Spacing = 10, Children = { listView } } }; } } }