在我的应用程序中,我已连接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
答案 0 :(得分:0)
您编写的代码是正常的。但是您可以减少一些步骤。
请参考以下使用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:RetireveRecordDemo"
x:Class="RetireveRecordDemo.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/RetireveRecordDemo
在Firebase数据库中有一篇关于CRUD的有用文章。 https://www.c-sharpcorner.com/article/xamarin-forms-working-with-firebase-realtime-database-crud-operations/