如何从Xamarin的Firebase中获取价值? [英] How to Get Value from Firebase in Xamarin?
问题描述
如何使用Xamarin Firebase.Database从Firebase实时数据库获取数据由于没有可用的文档,并且是新用户,因此需要使用Firebase。数据库
public void GetSubjects()
{
Database.Reference.Child(childName)。Ref.AddListenerForSingleValueEvent(new ValueEventListener());
public class ValueEventListener:Java.Lang.Object,Firebase.Database.IValueEventListener
{
public void OnCancelled(DatabaseError error)
{
throw new NotImplementedException();
public void OnDataChange(DataSnapshot snapshot)
{
//如何从快照获取值?
ClassName a = snapshot.GetValue(ClassName)//给出错误
$ b $ / code>
private void GetData()
{
FirebaseDatabase
.Instance
.Reference
.Child(将您的子节点名称放在这里)
.AddListenerForSingleValueEvent新的DataValueEventListener());
$ b class DataValueEventListener:Java.Lang.Object,IValueEventListener
{
public void OnCancelled(DatabaseError error)
{
//处理错误,但你必须
$ b $ public void OnDataChange(DataSnapshot snapshot)
if(snapshot.Exists())
{
DataModelClass model = new DataModelClass();
var obj = snapshot.Children;
$ b $ foreach(DataSnapshot snapshotChild in obj.ToEnumerable())
{
if(snapshotChild.GetValue(true)== null)continue;
model.PropertyName = snapshotChild.Child(把你的firebase属性名称放在这里)?GetValue(true)?
model.PropertyName = snapshotChild.Child(把你的firebase属性名称放在这里)?GetValue(true)?。ToString();
//根据需要使用类型转换。我已经使用了字符串属性
$ b
How to Get Data from Firebase Realtime Database using Xamarin Firebase.Database Android, as there is no documentation available and I am new user.
public void GetSubjects()
{
Database.Reference.Child("childName").Ref.AddListenerForSingleValueEvent(new ValueEventListener());
}
public class ValueEventListener : Java.Lang.Object, Firebase.Database.IValueEventListener
{
public void OnCancelled( DatabaseError error )
{
throw new NotImplementedException();
}
public void OnDataChange( DataSnapshot snapshot )
{
//How to Get Value from Snapshot?
ClassName a = snapshot.GetValue(ClassName) //Gives Error
}
解决方案 Look at this and see if it works...
private void GetData()
{
FirebaseDatabase
.Instance
.Reference
.Child("Put your child node name here")
.AddListenerForSingleValueEvent(new DataValueEventListener());
}
class DataValueEventListener: Java.Lang.Object, IValueEventListener
{
public void OnCancelled(DatabaseError error)
{
// Handle error however you have to
}
public void OnDataChange(DataSnapshot snapshot)
{
if (snapshot.Exists())
{
DataModelClass model = new DataModelClass();
var obj = snapshot.Children;
foreach (DataSnapshot snapshotChild in obj.ToEnumerable())
{
if (snapshotChild.GetValue(true) == null) continue;
model.PropertyName = snapshotChild.Child("Put your firebase attribute name here")?.GetValue(true)?.ToString();
model.PropertyName = snapshotChild.Child("Put your firebase attribute name here")?.GetValue(true)?.ToString();
// Use type conversions as required. I have used string properties only
}
}
}
}
这篇关于如何从Xamarin的Firebase中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!