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

I work with Xamarin, but still new to this, but I have a problem that I have the feeling that I shouldn't be. Here is 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 have a class on another page called "Man." This class has two properties: "FirstName" and "LastName". When I try to put it all together in the same way in Xamarin, I get an error: "The name" DisplayAlert "does not exist in the current context."

+8
c # listview xamarin
source share
3 answers

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 } } }; } } } 
+7
source share

DisplayAlert() is a method of the Page class.

In order for your class to use it, it needs to either create an instance of the Page object, or call it using this object or directly inherit it.

Since you stated that your Person class is actually also a Page class, you can also call it using one of the Person objects ie personObj.DisplayAlert(...)

Perhaps something similar to the following:

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

This, of course, assumes your Person class declaration looks something like this:

 public class Person : Page { ... } 

Obviously, the exact implementation will depend on how you structure your code. I just go over to what I see in your question and accept a few things.

+5
source share

or just try using:

 await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK"); 
+4
source share

All Articles