2017-04-19 65 views
0

如何使用Xamarin Firebase.Database获取来自Firebase实时数据库的数据Android,因为没有可用的文档,我是新用户。如何从Xamarin的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) 
    { 

     //How to Get Value from Snapshot? 
     ClassName a = snapshot.GetValue(ClassName) //Gives Error 

    } 
+0

您可以检查此:用火力地堡云消息远程通知(https://developer.xamarin.com/guides/机器人/ application_fundamentals /通知/远程的通知与 - FCM /#client_app)。 –

+0

我需要使用Firebase数据库,它们具有不同的apis集。 –

回答

1

看看这个,看看它的工作原理...

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 
      } 
     } 
    } 
}