我和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 }
}
};
}
}
}
显然,我在另一个名为" Person。"这个类有两个属性:" FirstName"和"姓氏。"当我尝试在Xamarin中将所有这些放在一起时,我得到的错误是:"名称' DisplayAlert'在当前上下文中不存在。"
答案 0 :(得分:7)
有两种方法可以解决这个问题,我倾向于第二种方法。接近Edward 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的新类: 使用系统; 使用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 }
}
};
}
}
}
答案 1 :(得分:6)
或尝试使用:
await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");
答案 2 :(得分:5)
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
{
...
}
显然,具体实现将取决于您如何构建代码。我只是按照你在问题中看到的内容并假设一些事情。