【问题标题】:How to show data stored in firebase RealtimeDataBase inside a new page in xamarin.forms?如何在 xamarin.forms 的新页面中显示存储在 firebase Realtime DataBase 中的数据?
【发布时间】:2020-05-11 23:18:01
【问题描述】:

我已经创建了一个实时数据库,我在其中存储了一些数据,例如公司的名称。 当我点击“存储数据”时,我进入一个标签页。那么,我如何才能像 tabbedPage 的标题一样仅将公司名称可视化?

【问题讨论】:

  • 您知道如何在 Firebase 中保存数据,但不知道如何查询?你试过什么?有很多现有的问题可以解决这个问题,例如:stackoverflow.com/questions/58881101/…
  • 我保存了一些数据,但我无法将其中一个可视化到另一个页面中
  • 特别是我想通过 xaml 来展示它,这可能吗?
  • 是的。从哪里获取数据并不重要。同样,有很多现有的问题和文档以及与 XAML 进行数据绑定的示例。
  • 你能帮我吗?我找不到这个在 xaml 上绑定的例子

标签: c# database firebase xamarin.forms data-retrieval


【解决方案1】:

您只需要从您的 RealtimeDataBase 中获取数据,然后将其绑定到您的页面,

例如:

模型公司

public class Company
{
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }

}

创建FireBaseHelp,操作firabase:

public class FirebaseHelper
{
    FirebaseClient firebase = new FirebaseClient("https://androiddemo-f9385.firebaseio.com/");

    public async Task<List<Company>> GetAllCompanies()
    {

        return (await firebase
          .Child("Companies")
          .OnceAsync<Company>()).Select(item => new Company
          {
              CompanyName = item.Object.CompanyName,
              CompanyId = item.Object.CompanyId
          }).ToList();
    }

    public async Task AddCompay(int companyId,string companyName)
    {

        await firebase
          .Child("Companies")
          .PostAsync(new Company() { CompanyId = companyId, CompanyName = companyName});
    }

    public async Task<Company> GetCompany(int companyId)
    {
        var allCompanies = await GetAllCompanies();
        await firebase
          .Child("Companies")
          .OnceAsync<Company>();
        return allCompanies.Where(a => a.CompanyId == companyId).FirstOrDefault();
    }

}

创建标签页CompanyPage

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CompanyPage : TabbedPage
{
    public CompanyPage(List<Company> allCompanies)
    {
        foreach (var item in allCompanies)
        {
            Children.Add(new MyCompanyPage(item));
        }
    }
}

创建 MyCompanyPage.xaml.cs ,标签页的子级(在此处设置 BindingContext 以绑定您的数据)。

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyCompanyPage : ContentPage
{
    public MyCompanyPage(Company company)
    {
        InitializeComponent();
        BindingContext = company;
    }
}

MyCompanyPage.xaml,根据需要绑定 companyName:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         Title="{Binding CompanyName}"
         x:Class="XamarinFirebase.MyCompanyPage">
 <ContentPage.Content>
    <StackLayout>
        <Label Text="{Binding CompanyName}"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>
 </ContentPage.Content>
</ContentPage>

然后在MainPage中,当你点击一个按钮时,它会查询FireBase并获取数据。

  private async void BtnRetrive_Clicked(object sender, EventArgs e)
    {          
        var allCompanies = await firebaseHelper.GetAllCompanies();
        Navigation.PushAsync(new CompanyPage(allCompanies));
    }

您可以阅读Data Binding 和 这是效果的 gif(添加三个 cpmpanies 并在标签页中显示它们):

【讨论】:

  • 是的,当我注册时我可以看到公司名称,但是当我注销时,然后登录我看不到任何公司名称。我能做什么?
  • 我认为这与Firebase Authentication 有关,您是否为您的Firebase Realtime Database 确定了一些Security Rules?我对此了解不多。这是另一个问题,您可以打开一个新问题。
  • @EdoardoDellaSeta 好的,我会做一些研究,如果我有任何反馈或建议,我会在新问题中回复你。如果这个问题的答案对您有用,您能否将其标记为接受解决方案?
  • 完美的 i 标记,但我的声望低于 15
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 2019-04-10
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多