从绑定ListView Xamarin表单检索firebase数据

从绑定ListView Xamarin表单检索firebase数据,listview,firebase-realtime-database,xamarin.forms,binding,Listview,Firebase Realtime Database,Xamarin.forms,Binding,我有一个listview,其中我使用一个助手从firebase检索数据,并通过将它们绑定到.xaml中来显示数据,如下所示: 助手: public class FirebaseHelper public async Task<List<Books>> GetAllBooks() { return (await firebase .Child("Books")

我有一个listview,其中我使用一个助手从firebase检索数据,并通过将它们绑定到.xaml中来显示数据,如下所示:

助手:

public class FirebaseHelper
public async Task<List<Books>> GetAllBooks()
        {
            return (await firebase
              .Child("Books")
              .OnceAsync<Books>()).Select(item => new Books
              {
                  Title = item.Object.Title,
                  Author = item.Object.Author,
                  Genre = item.Object.Genre,
                  Cover  = item.Object.Cover
              }).ToList();
        }


public async Task<string> getTitle()
        {
            return (await firebase
              .Child("Books")
              .OnceAsync<Books>()).Select(item => new Books
              {
                  Title = item.Title
              }).ToString();
        }


公共类FirebaseHelper
公共异步任务GetAllBooks()
{
返回(等待火力基地)
.儿童(“书籍”)
.OnceAsync())。选择(项=>新书
{
Title=item.Object.Title,
Author=item.Object.Author,
流派=item.Object.Genre,
封面=item.Object.Cover
}).ToList();
}
page.xaml.cs

public List<Books> libriall;

protected async override void OnAppearing()
        { 
            base.OnAppearing();
            bookall = await firebaseHelper.GetAllBooks();
            listbook.ItemsSource = bookall;
        }

公共列表库;
受保护的异步重写void OnAppearing()
{ 
base.OnAppearing();
bookall=等待firebaseHelper.GetAllBooks();
listbook.ItemsSource=bookall;
}
以及.xaml文件中listview listbook的一部分:

<Label 
       TextColor="#33272a"
       Text="{Binding Title}"
       x:Name="bTitle"/>

好的,现在我在ViewCell中放了一个按钮,我想获取书名并在PostAsync中使用它,所以我基本上需要获取单个书名并将其放入字符串中

已在帮助器中创建类似的方法:

public class FirebaseHelper
public async Task<List<Books>> GetAllBooks()
        {
            return (await firebase
              .Child("Books")
              .OnceAsync<Books>()).Select(item => new Books
              {
                  Title = item.Object.Title,
                  Author = item.Object.Author,
                  Genre = item.Object.Genre,
                  Cover  = item.Object.Cover
              }).ToList();
        }


public async Task<string> getTitle()
        {
            return (await firebase
              .Child("Books")
              .OnceAsync<Books>()).Select(item => new Books
              {
                  Title = item.Title
              }).ToString();
        }



公共异步任务getTitle()
{
返回(等待火力基地)
.儿童(“书籍”)
.OnceAsync())。选择(项=>新书
{
标题=项目。标题
}).ToString();
}

但是我不知道如何将书籍属性链接到它们通过绑定显示的单个viewcell,知道吗?

您不需要再次从您的服务获取数据,您的列表中已经有了它

void ButtonClicked(object sender, EventArgs args)
{
  Button btn = (Button)sender;

  // the BindingContext of the Button will be the Books
  // object for the row you clicked on 
  var book = (Books)btn.BindingContext;

  // now you can access all of the properties of Books
  var title = book.Title;
}

如果
GetAllBooks
检索标题,为什么还需要另一个方法
getTitle
来获取标题?GetAllBooks返回一个列表,getTitle返回一个字符串而不是一个字符串Yes,但是列表包含一个图书列表,并且每本书都有一个title属性。为什么你需要再次检索它,而不是仅仅从你已经拥有的书中提取它?您正在单击列表中的某一行,只想获取该行的图书标题?我只想获取与之交互的行的标题,如果使用GetAllBooks,我将获取所有图书及其属性的列表,而我只需要一个字符串。您无需再次调用
GetAllBooks
,您在
ItemsSource