Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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."

like image 328
prinsJoe Avatar asked Dec 16 '14 04:12

prinsJoe


3 Answers

or just try to use :

await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");
like image 151
Badr Bellaj Avatar answered Nov 10 '22 15:11

Badr Bellaj


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

        };
    }

}
}
like image 10
JamesMontemagno Avatar answered Nov 10 '22 14:11

JamesMontemagno


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.

like image 5
Edward L. Avatar answered Nov 10 '22 15:11

Edward L.