我无法使用 FireSharp 从 Firebase 检索数据到 Xamarin.Android
I cant retrieve data from Firebase to Xamarin.Android using FireSharp
我正在使用 firesharp,我尝试在我的移动应用程序中检索数据,但是当我尝试它时,应用程序在显示文本之前就开始崩溃了。有什么办法我做错了什么吗?或者只是 firesharp 不应该用于 Xamarin。
我已经使用具有插入和检索功能的程序完成了此操作,但在这里我只想检索然后插入
主要活动:
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>
数据class:
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; }
}
}
我已经使用具有插入和检索功能的程序完成了此操作,但在这里我只想检索然后插入
您可以在其他方法中移动 client.GetAsync("question/"+g) 并且不要对 OnCreate 方法使用异步,然后再次尝试您的项目。
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;
}
我正在使用 firesharp,我尝试在我的移动应用程序中检索数据,但是当我尝试它时,应用程序在显示文本之前就开始崩溃了。有什么办法我做错了什么吗?或者只是 firesharp 不应该用于 Xamarin。 我已经使用具有插入和检索功能的程序完成了此操作,但在这里我只想检索然后插入
主要活动:
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>
数据class:
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; }
}
}
我已经使用具有插入和检索功能的程序完成了此操作,但在这里我只想检索然后插入
您可以在其他方法中移动 client.GetAsync("question/"+g) 并且不要对 OnCreate 方法使用异步,然后再次尝试您的项目。
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;
}