如何从 Xamarin 中的 Firebase 获取价值? [英] How to Get Value from Firebase in Xamarin?

查看:25
本文介绍了如何从 Xamarin 中的 Firebase 获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Xamarin Firebase.Database Android 从 Firebase 实时数据库获取数据,因为没有可用的文档并且我是新用户.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆