You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android中DatabaseReference未定义GetValueAsync方法求助

Xamarin Firebase Database 读取数据时 GetValueAsync 方法未定义问题

我用Visual Studio + C#开发Android应用,计划通过Firebase读写数据,写入功能正常,但读取时遇到问题:无论在代码何处调用GetValueAsync()方法,都会报错提示该方法未在DatabaseReference中定义。

具体错误提示:

'DatabaseReference' doesn't contain a definition for "GetValueAsync" and we couldn't find any extension method "GetValueAsync" that accepts a first argument of type 'DatabaseReference' (are you forgetting to use a directive or an assembly reference?)

我的代码示例:

using Android.Gms.Common.Apis;
using Firebase;
using Firebase.Database;
using Java.Util.Prefs;
using Newtonsoft.Json;
using static Android.Telephony.CarrierConfigManager;
using Xamarin.Essentials;

namespace AppVanillaRTB
{
    [Activity(Label = "@string/app_name", MainLauncher = true)]
    public class MainActivity : Activity
    {
        DatabaseReference databaseRef;

        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            FirebaseApp.InitializeApp(this);
            
            databaseRef = FirebaseDatabase.Instance.Reference;

            Button ativar = FindViewById<Button>(Resource.Id.button1);
            EditText Nome = FindViewById<EditText>(Resource.Id.editText1);

            int order;
           
            ativar.Click += async (sender, e) =>
            {
                if (string.IsNullOrEmpty(Nome.Text)) 
                {

                }
                else
                {
                    var newData = new UserData
                    {
                        Nome = Nome.Text,
                        Cod = 0
                    };

                    string x = "";
                    for (int i = 0; i < 10; i++)
                     {
                         string y = "";
                         x = i.ToString();
                         var dataSnapshot = await databaseRef.Child("DELIV").Child(x).Child("Nome").GetValueAsync();

                         y = dataSnapshot.ToString();
                         if (y == "") 
                         {
                             break;
                         }
                     }

                    await databaseRef.Child("DELIV").Child(x).Child("Nome").SetValueAsync(newData.Nome);
                    await databaseRef.Child("DELIV").Child(x).Child("Cod").SetValueAsync(newData.Cod);
                                        
                    Xamarin.Essentials.Preferences.Set("Nome", Nome.Text);
                    Xamarin.Essentials.Preferences.Set("Code", newData.Cod);
                    //Xamarin.Essentials.Preferences.Set("Order",x);
                    
                }
            };
        }
    }
    
    public class UserData
    {
        public string Nome { get; set; }
        public int Cod { get; set; }
    }
}

我已经尝试过重装Firebase包、新建异步方法等方案,但都没能解决问题。目前改用FireSharp API暂时解决了问题,但需要重构全部代码,希望能找到基于原Xamarin.Firebase.Database API的解决方案。

内容的提问来源于stack exchange,提问作者Ricardo Souto Princhak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:12:12