Xamarin Forms "...DisplayAlert does not exist in the current context."

DisplayAlert() is a method of the Page class.

In order for your class to be able to use it, it needs to either instantiate a Page object and invoke it using that object or directly inherit from it.

Since you stated that your Person class is actually also a Page class, you could also invoke it using one of your Person objects i.e. personObj.DisplayAlert(...)

Perhaps something similar to the following:

var personObj = new Person();
personObj.DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");

This of course, assumes that your Person class declaration goes something like the following:

public class Person : Page
{
    ...
}

Obviously, the exact implementation will depend on how you structure your code. I am just going by what I can see in your question and assuming a few things.


or just try to use :

await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");

There are two ways to solve this and I lean toward the second one. Close to what Edward L. has said.

DisplayAlert is a method on a Xamarin.Forms Page... and you are inside of a static method that is returning that page, so you have no reference to it.

So you could 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 should really 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 Page:

using 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 }
            }

        };
    }

}
}