【问题标题】:Unable to retrieve records from Firebase in Xamarin c#无法从 Xamarin c# 中的 Firebase 检索记录
【发布时间】:2019-05-11 07:08:02
【问题描述】:

在我的应用程序中,我已连接 Firebase 并尝试填充列表视图,但在开始时,我没有看到任何记录。它只是显示一个空白页面,列表视图中没有填充任何内容。

这是我的 firebasehelper.cs

{
class FirebaseHelper
{
    FirebaseClient firebase = new FirebaseClient("https://mylimo-b2029.firebaseio.com/");


    public async Task<List<users>> GetAllUsers()
    {

        try
        {
          return (await firebase
         .Child("Users")
         .OnceAsync<users>()).Select(item => new users
         {
             //,

             first_name = item.Object.first_name,
             last_name = item.Object.last_name,
             password = item.Object.password,
             user_id = item.Object.user_id,
         }).ToList();
        }
        catch (Exception ex)
        {
            throw new Exception("GetUsers  Additional information..." + ex, ex);
        }
    }    


    public async Task<users> GetUsers(string usr_id)
    {
        try
        {
            var allUsers = await GetAllUsers();
            await firebase
              .Child("users")
              .OnceAsync<users>();
            return allUsers.Where(a => a.user_id == usr_id).FirstOrDefault();
        }


        catch (Exception ex)
        {
            throw new Exception("GetUsers  Additional information..." + ex, ex);
        }
    }
}

下面是 MainPage.xaml.cs

 public partial class MainPage : ContentPage
{
    FirebaseHelper firebaseHelper = new FirebaseHelper();
    public MainPage()
    {
        try
        {
            InitializeComponent();
        }
        catch (Exception ex)
        {
            throw new Exception("InitializeComponent  Additional information..." + ex, ex);
        }
    }

    protected async override void OnAppearing()
    {
        try
        {
            base.OnAppearing();
            var allUsers = await firebaseHelper.GetAllUsers();
            lstPersons.ItemsSource = allUsers;
        }

        catch (Exception ex)
        {
            throw new Exception("OnAppearing  Additional information..." + ex, ex);
        }




    }

在应用启动时,它只会显示一个没有任何记录的空白列表视图控件,

.

我的firebase数据库如下]1

【问题讨论】:

    标签: c# firebase xamarin


    【解决方案1】:

    你的代码是正常的。但是你可能会少一些步骤。

    Firebase 数据库的使用请参考以下方法。

    首先,请打开firebase数据库控制台,将读/写身份验证规则设置为null,如下图所示。

    然后创建一个模块user

    public class User
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
    
        public string password { get; set; }
    
        public int userid { get; set; }
    }
    

    有我的 firebasehelper。

    public class Firebasehelper
    {
        FirebaseClient firebase;
    
        public Firebasehelper()
        {
            firebase = new FirebaseClient("https://fir-databasedemo-62a72.firebaseio.com/");
        }
       
        public async Task AddUser(int userid, string first_name, string last_name, string password)
        {
            await firebase
              .Child("Users")
              .PostAsync(new User() { userid = userid, first_name = first_name, last_name= last_name, password= password });
        }
    
        public async Task<List<User>> GetAllUsers()
        {
            return (await firebase
              .Child("Users")
              .OnceAsync<User>()).Select(item => new User
              {
    
                  userid = item.Object.userid,
                  first_name = item.Object.first_name,
                  last_name = item.Object.last_name,
                  password = item.Object.password
              }).ToList();
        }
    }
    

    请不要直接在 firebase 中创建记录。

    我们应该通过代码添加这些记录。成功添加这些记录后,您可以在firebase控制台中看到这些记录。

    与以下代码一起使用。

      private async void BtnAdd_Clicked(object sender, EventArgs e)
      {
            // AddUser(int userid, string first_name, string last_name, string password)
            await firebaseHelper.AddUser(Convert.ToInt32(1), "Jon","hard","123.com");
            await firebaseHelper.AddUser(Convert.ToInt32(1), "Leon", "esay", "1234.com");
            await firebaseHelper.AddUser(Convert.ToInt32(1), "Rebecca", "middlue", "12345.com");
            await DisplayAlert("Success", "Person Added Successfully", "OK");
           var allPersons = await firebaseHelper.GetAllUsers();
           lstPersons.ItemsSource = allPersons;
        }
    

    这是我的MainPage.xaml

     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:RetrieveRecordDemo"
             x:Class="RetrieveRecordDemo.MainPage">
      <StackLayout>
        <Button x:Name="BtnAdd" Text="add" />
    
        <ListView x:Name="lstPersons">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
    
                            <Label Text="{Binding first_name}"  > 
                     </Label>
                            <Label Text="{Binding last_name}" > 
                     </Label>
                         
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    </ContentPage>
    

    MainPage.xml.cs

       public partial class MainPage : ContentPage
    {
        Firebasehelper firebaseHelper;
        public MainPage()
        {
            InitializeComponent();
            firebaseHelper = new Firebasehelper();
    
            BtnAdd.Clicked += BtnAdd_Clicked;
        }
    
        private async void BtnAdd_Clicked(object sender, EventArgs e)
        {
            // AddUser(int userid, string first_name, string last_name, string password)
            await firebaseHelper.AddUser(Convert.ToInt32(1), "Jon","hard","123.com");
            await firebaseHelper.AddUser(Convert.ToInt32(1), "Leon", "esay", "1234.com");
            await firebaseHelper.AddUser(Convert.ToInt32(1), "Rebecca", "middlue", "12345.com");
            await DisplayAlert("Success", "Person Added Successfully", "OK");
           var allPersons = await firebaseHelper.GetAllUsers();
           lstPersons.ItemsSource = allPersons;
        }
    
        protected async override void OnAppearing()
        {
    
            base.OnAppearing();
    
    
            try
            {
                base.OnAppearing();
                var allUsers = await firebaseHelper.GetAllUsers();
                lstPersons.ItemsSource = allUsers;
            }
    
            catch (Exception ex)
            {
                throw new Exception("OnAppearing  Additional information..." + ex, ex);
            }
        }
    }
    

    有运行截图。

    这是我的演示:https://github.com/851265601/RetrieveRecordDemo

    firebase 数据库中有一篇关于 CRUD 的有用文章。

    https://www.c-sharpcorner.com/article/xamarin-forms-working-with-firebase-realtime-database-crud-operations/

    【讨论】:

      猜你喜欢
      • 2011-08-22
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多