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

23,953

Solution 1

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

        };
    }

}
}

Solution 2

or just try to use :

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

Solution 3

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.

Share:
23,953

Related videos on Youtube

prinsJoe
Author by

prinsJoe

BY DAY: Super rockin' Xamarin Intern over at ThinkDigital! BUT BY NIGHT: ... Yeah, still a super rockin' Xamarin Intern over at ThinkDigital!

Updated on July 05, 2022

Comments

  • prinsJoe
    prinsJoe almost 2 years

    I'm working with Xamarin, still new to it, but I'm having a problem that I get the feeling I shouldn't be. Here's my problem:

    using System;
    using Xamarin.Forms;
    
    namespace DataBinding_Lists
    {
    public class App
    {
        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 DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
            };
    
            return new ContentPage { 
                Content = new StackLayout
                {
                    Padding = new Thickness (5,20,5,5),
                    Spacing = 10,
                    Children = { listView }
                }
            };
        }
    
    }
    

    }

    Obviously, I've got a class on another page called "Person." This class has two properties: "FirstName" and "LastName." When I try and put all of this together like so in Xamarin, I get the error saying: "The name 'DisplayAlert' does not exist in the current context."

  • prinsJoe
    prinsJoe over 9 years
    I've been working straight out of Xamarin tutorials, so it was kinda annoying that I had to go through all of this, but you private static Page page idea was exactly what I needed! Thanks!
  • Hagbard
    Hagbard over 5 years
    That works for me as well. Please note that calling this from other threads would have to be done as follows: Device.BeginInvokeOnMainThread (async () => await Application.Current.MainPage.DisplayAlert("My Alert", "My message.", "OK") );
  • Andrew the Programmer
    Andrew the Programmer over 3 years
    Simplest solution presented here, no need to introduce any sort of unnecessary complexities