Xamarin表单"... DisplayAlert在当前上下文中不存在."

pri*_*Joe 9 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 }
            }
        };
    }

}
Run Code Online (Sandbox Code Playgroud)

}

显然,我在另一个名为"人物"的页面上有一个班级.该类有两个属性:"FirstName"和"LastName".当我尝试在Xamarin中将所有这些放在一起时,我得到错误说:"当前上下文中不存在名称'DisplayAlert'."

Jam*_*gno 9

有两种方法可以解决这个问题,我倾向于第二种方法.接近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;
    }

}
}
Run Code Online (Sandbox Code Playgroud)

你应该做的是创建一个新的类作为你的页面.

你的App.cs变成了这个:

using System;
using Xamarin.Forms;

namespace DataBinding_Lists
{
public class App
{
    public static Page GetMainPage ()
    {   
        return new PeoplePage();
    }

}
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个继承自Page:using System的新类; 使用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 }
            }

        };
    }

}
}
Run Code Online (Sandbox Code Playgroud)


Bad*_*laj 8

或者只是尝试使用:

await App.Current.MainPage.DisplayAlert("Alert", "your message", "OK");
Run Code Online (Sandbox Code Playgroud)

  • 这也适用于我。请注意,从其他线程调用它必须按如下方式完成: Device.BeginInvokeOnMainThread (async () => await Application.Current.MainPage.DisplayAlert("My Alert", "My message.", "OK") ) ; (2认同)

Edw*_* L. 5

DisplayAlert()是一种Page类的方法.

为了使您的类能够使用它,它需要实例化一个Page对象并使用该对象调用它或直接从它继承.

既然你声明你的Person类实际上也是一个Page类,你也可以使用你的一个Person对象来调用它i.e. personObj.DisplayAlert(...)

也许类似于以下内容:

var personObj = new Person();
personObj.DisplayAlert ("Tapped!", e.SelectedItem + " was tapped.", "OK", "");
Run Code Online (Sandbox Code Playgroud)

当然,这假设您的Person类声明类似于以下内容:

public class Person : Page
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

显然,具体的实现将取决于您如何构建代码.我只是按照你在问题中看到的内容并假设一些事情.