我无法使用FireSharp将数据从Firebase检索到Xamarin.Android

时间:2019-02-09 13:47:15

标签: c# android firebase-realtime-database xamarin.android fire-sharp

我正在使用firesharp,我尝试在移动应用程序中检索数据,但是当我尝试使用它时,该应用程序在显示文本之前在启动时崩溃。我做错了什么吗?或仅仅是锐利不应该用于Xamarin。 我已经使用具有插入和检索功能的程序进行了此操作,但是在这里我只想检索然后插入

MainActivity:

 using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using FireSharp.Config;
    using FireSharp.Interfaces;
    using FireSharp.Response;
    using System.Threading;
    using System;

    namespace Quiz__
    {
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
        public class MainActivity : AppCompatActivity
        {

            IFirebaseConfig config = new FirebaseConfig
            {
                AuthSecret = "Im hiding it",
                BasePath = "Im hiding it"
            };
            IFirebaseClient client;
            protected override async void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                client = new FireSharp.FirebaseClient(config);
                TextView txtQuestion = FindViewById<TextView>(Resource.Id.textQuestion);
                var g = 2;
//g variable is just for the test
                FirebaseResponse response = await client.GetAsync("question/"+g);
                Data obj = response.ResultAs<Data>();
                txtQuestion.Text = obj.question;

            }
        }
    }

AXML:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">

        <TextView
            android:id="@+id/textQuestion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="35sp"
        />

    </RelativeLayout>

数据类:

namespace Quiz__
{
    internal class Data
    {
        public string Id { get; set; }
        public string question { get; set; }
        public string answer { get; set; }
        public string hint { get; set; }
    }
}

我已经使用具有插入和检索功能的程序进行了此操作,但是在这里我只想检索然后插入

1 个答案:

答案 0 :(得分:0)

您可以使用其他方法移动client.GetAsync(“ question /” + g),并且不要对OnCreate方法使用async,然后重试项目。

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        client = new FireSharp.FirebaseClient(config);
        txtQuestion = FindViewById<TextView>(Resource.Id.textQuestion);

        //g variable is just for the test
        test();
    }

    public async void test()
    {
        var g = "test1";
        FirebaseResponse response = await client.GetAsync("question/" + g);
        Data obj = response.ResultAs<Data>();
        txtQuestion.Text = obj.question;
    }