[英]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.我正在使用 Xamarin,对它还是陌生的,但我有一个问题,我觉得我不应该这样做。 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."
这个类有两个属性:“FirstName”和“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."
当我尝试像在 Xamarin 中那样将所有这些放在一起时,我收到错误消息:“当前上下文中不存在名称‘DisplayAlert’。”
或者只是尝试使用:
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.
接近 Edward L. 所说的。
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. DisplayAlert
是 Xamarin.Forms 页面上的一个方法......并且您处于返回该页面的静态方法内部,因此您没有对它的引用。
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:你的
App.cs
变成了这个:
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
:然后创建一个继承自
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 }
}
};
}
}
}
DisplayAlert()
is a method of the Page
class. DisplayAlert()
是Page
类的一个方法。
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.为了让您的类能够使用它,它需要实例化一个
Page
对象并使用该对象调用它,或者直接从它继承。
Since you stated that your Person
class is actually also a Page
class, you could also invoke it using one of your Person
objects ie personObj.DisplayAlert(...)
由于您声明您的
Person
类实际上也是一个Page
类,您还可以使用您的Person
对象之一调用它, 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 that your Person class declaration goes something like the following:当然,这假设您的 Person 类声明如下所示:
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.
我只是根据我在您的问题中看到的内容并假设一些事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.