2017-04-19 31 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 
      } 
     } 
    } 
}