【问题标题】:Xamarin Forms "...DisplayAlert does not exist in the current context."Xamarin 表单“...DisplayAlert 在当前上下文中不存在。”
【发布时间】:2015-02-14 08:25:36
【问题描述】:

我正在与 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'。”

【问题讨论】:

  • 该错误告诉您 Xamarin 不知道 DisplayAlert 是什么。您可能缺少对程序集的引用、缺少 using 语句或两者兼而有之。
  • DisplayAlert 方法由“Xamarin.Forms”使用,如上所述,我正在使用该方法。 DisplayAlert 是由 Xamarin 创建并为 Xamarin 创建的方法,所以如果我使用它告诉我的方法,那么我不知道为什么它仍然给我这个错误。 (developer.xamarin.com/guides/cross-platform/xamarin-forms/…)
  • 是的,但它是Page 类的方法(不确定它是静态方法还是成员方法),但您既没有使用Page 前缀调用它,也没有使用App 类继承来自Page 类,所以它无法知道如何调用它。您要么必须执行 Page.DisplayAlert()(如果它是静态方法),要么让您的 App 类继承自 Page 并保持当前的调用方式。
  • 我刚刚尝试了这两个想法。它们听起来很合法,好像它们应该工作,但我尝试了这两个想法,它说的是同一件事:“一个对象访问非静态成员 'Xamarin.Forms.Page.DisplayAlert(string,string,string,string)' 需要引用,这是否意味着我应该创建一个对象来调用该方法,而不是使用静态方法?
  • 是的,所以看起来DisplayAlert() 是一个成员方法。因此,您可能不会从静态上下文中调用它。所以是的,您必须使用您的一个对象来调用它,请参阅答案。

标签: c# listview xamarin


【解决方案1】:

或者只是尝试使用:

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

【讨论】:

  • 这也适用于我。请注意,从其他线程调用它必须按如下方式完成: Device.BeginInvokeOnMainThread (async () => await Application.Current.MainPage.DisplayAlert("My Alert", "My message.", "OK") ) ;
  • 这里介绍的最简单的解决方案,无需引入任何不必要的复杂性
【解决方案2】:

有两种方法可以解决这个问题,我倾向于第二种。接近 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的新类:

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 }
            }

        };
    }

}
}

【讨论】:

  • 我一直在直接学习 Xamarin 教程,所以我不得不完成所有这些工作有点烦人,但你的 私有静态页面 想法正是我需要什么!谢谢!
【解决方案3】:

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
{
    ...
}

显然,确切的实现将取决于您如何构建代码。我只是根据我在您的问题中看到的内容并假设一些事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多