Xamarin形式“..DisplayAlert 在当前上下文中不存在
本文关键字:上下文 不存在 形式 DisplayAlert Xamarin | 更新日期: 2023-09-27 18:34:27
我正在使用 Xamarin,对它仍然很陌生,但我遇到了一个问题,我感觉我不应该这样做。这是我的问题:
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 }
}
};
}
}
}
显然,我在另一个页面上有一个名为"人"的类。此类有两个属性:"名字"和"姓氏"。当我尝试在 Xamarin 中像这样将所有这些放在一起时,我收到错误消息:"名称'DisplayAlert'在当前上下文中不存在。
或者只是尝试使用:
await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");
有两种方法可以解决这个问题,我倾向于第二种。接近爱德华L.所说的话。
DisplayAlert
是 Xamarin.Forms 页面上的一种方法...并且您位于返回该页面的静态方法中,因此您没有对它的引用。
所以你可以这样做:
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;
}
}
}
您真正应该做的是创建一个新类,它是您的页面。
您的App.cs
变成这样:
using System;
using Xamarin.Forms;
namespace DataBinding_Lists
{
public class App
{
public static Page GetMainPage ()
{
return new PeoplePage();
}
}
}
然后创建一个继承自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()
是Page
类的一种方法。
为了使您的类能够使用它,它需要实例化一个Page
对象并使用该对象调用它,或者直接从它继承。
由于您声明您的 Person
类实际上也是一个Page
类,因此您也可以使用您的 Person
对象之一来调用它i.e. personObj.DisplayAlert(...)
也许类似于以下内容:
var personObj = new Person();
personObj.DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
当然,这假设您的 Person 类声明如下所示:
public class Person : Page
{
...
}
显然,确切的实现将取决于您如何构建代码。我只是按照我在你的问题中看到的,并假设一些事情。