c# - Xamarin 表格 "...DisplayAlert does not exist in the current context."

标签 c# listview xamarin

我正在使用 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”和“LastName”。当我尝试像在 Xamarin 中那样将所有这些放在一起时,我收到错误消息:“名称‘DisplayAlert’在当前上下文中不存在。”

最佳答案

或者只是尝试使用:

await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");

关于c# - Xamarin 表格 "...DisplayAlert does not exist in the current context.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497633/

相关文章:

c# - 为什么 mvccontrib 的 AssertViewRendered().ForView ("Edit") 由于 View 名称是完整的 cshtml 路径而失败?

flutter - 将数据发送到新屏幕,但不知道如何设置

ios - 在 iOS 中忽略 TableView 标题内的按钮单击

c# - 选择行时修改 ListView 的行行为

android:更改方向时如何在 arrayadapter/listview 中保留数据?

android - 三星更新后的错误 |单人游戏 |将 viewport.height 检测为设备宽度

android - Xamarin Android 文件系统路径

c# - 将数据放在 cookie 中安全吗?

c# - 如何在 C# 中截断列表?

c# - "if (a() && b != null)""a()"会一直被评估吗?