2017-04-19 3 views
0

So erhalten Sie Daten aus der Firebase Realtime Database mit Xamarin Firebase.Database Android, da keine Dokumentation verfügbar ist und ich ein neuer Benutzer bin.Wie bekomme ich den Wert von Firebase in Xamarin?

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

Sie können dies überprüfen: [Remote Benachrichtigungen mit Firebase Cloud Messaging] (https://developer.xamarin.com/guides/ android/application_fundamentals/benachrichtigungen/remote-notifications-with-fcm/# client_app). –

+0

Ich brauche Firebase Database, die verschiedene apis haben. –

Antwort

1

Blick auf diese und sehen, ob es funktioniert ...

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