SQLite Xamarin.Forms没有实例化数据库

时间:2018-02-14 16:21:54

标签: c# sqlite xamarin xamarin.forms

我按照本指南将SQLite添加到我的Xamarin.Forms共享资产项目中。它不是PCL。我正在使用XAML和Code Behind。

我似乎在所有正确的地方都拥有所有代码。我有一个SQLite.cs,它包含我想要制作的所有数据库调用,并且还定义了数据库。

我在一个我知道可以工作的类中定义了一个接口,并且可以访问它,因为其中正在使用其他函数。

然后我在每个平台项目中都有一个类(我在droid示例下面显示),它使用该接口获取本地文件路径。

最后在我的共享资产项目中,我定义并(应该)实例化(我认为)我的数据库。

所有这一切,在我的一个ViewModel中,我尝试执行数据库调用。但是,内部异常表明我收到了对象引用错误。基本上我的数据库"对象在尝试执行database.MyMethod()时为null。

我无法弄清楚为什么它没有被实例化。有什么想法吗?

它执行的行是

var dbtimings = TechsportiseData.GetTimingsAsync().Result.ToList();

那种方法是......

public static Task<List<Timing>> GetTimingsAsync()
{
    return database.Table<Timing>().ToListAsync();
}

因为database为空而绊倒。

SQLite.cs(共享资产项目)

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
using static TechsportiseApp.Helpers.GlobalFunctions;
using TechsportiseApp.Models;
using System.Threading.Tasks;
using SQLite;

namespace TechsportiseApp.Data
{
    public class TechsportiseData
    {
        readonly SQLiteAsyncConnection database;

        public TechsportiseData(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<Scan>().Wait();
            database.CreateTableAsync<Timing>().Wait();
        }


        public static Task<List<Timing>> GetTimingsAsync()
        {
            return database.Table<Timing>().ToListAsync();
        }

        public static Task<List<Timing>> GetTimingsNotUploadedAsync()
        {
            return database.QueryAsync<Timing>("SELECT * FROM [Timing] WHERE [Uploaded] = 0");
        }

        public static Task<Timing> GetTimingAsync(int id)
        {
            return database.Table<Timing>().Where(i => i.Id == id).FirstOrDefaultAsync();
        }

        public static Task<int> SaveTimingAsync(Timing timing)
        {
            if (timing.Id != 0)
            {
                return database.UpdateAsync(timing);
            }
            else
            {
                return database.InsertAsync(timing);
            }
        }

        public static Task<int> DeleteTimingAsync(Timing timing)
        {
            return database.DeleteAsync(timing);
        }

        public static Task DeleteAllTimingsAsync()
        {
            database.DropTableAsync<Timing>().Wait();          
            return database.CreateTableAsync<Timing>();
        }

        public static Task<List<Scan>> GetScansAsync()
        {
            return database.Table<Scan>().ToListAsync();
        }

        public static Task<List<Scan>> GetScansNotUploadedAsync()
        {
            return database.QueryAsync<Scan>("SELECT * FROM [Scan] WHERE [Uploaded] = 0");
        }

        public static Task<Scan> GetScanAsync(int id)
        {
            return database.Table<Scan>().Where(i => i.Id == id).FirstOrDefaultAsync();
        }

        public static Task<int> SaveScanAsync(Scan scan)
        {
            if (scan.Id != 0)
            {
                return database.UpdateAsync(scan);
            }
            else
            {
                return database.InsertAsync(scan);
            }
        }

        public static Task<int> DeleteScanAsync(Scan scan)
        {
            return database.DeleteAsync(scan);
        }

        public static Task DeleteAllScansAsync()
        {
            database.DropTableAsync<Scan>().Wait();
            return database.CreateTableAsync<Scan>();
        }



        public static Task<RaceClock> GetRaceClockAsync()
        {
            return database.Table<RaceClock>().FirstOrDefaultAsync();
        }

        public static Task<int> SaveRaceClockAsync(RaceClock clock)
        {
            return database.InsertAsync(clock);
        }

        public static Task DeleteRaceClockAsync()
        {
            database.DropTableAsync<RaceClock>().Wait();
            return database.CreateTableAsync<RaceClock>();
        }
    }
}

App.xaml.cs

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.Generic;
using TechsportiseApp.API;
using TechsportiseApp.Views;
using TechsportiseApp.ViewModels;
using TechsportiseApp.Models;
using TechsportiseApp.Helpers;
using TechsportiseApp.Data;
using static TechsportiseApp.Helpers.GlobalFunctions;

//[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace TechsportiseApp
{
    public partial class App : Application
    {
        static TechsportiseData database;



        public App()
        {

            InitializeComponent();


            Application.Current.Properties["APIServer"] = "https://www.techsportise.com/";
            //Application.Current.Properties["APIServer"] = "http://localhost:52693/";

            if (Application.Current.Properties.ContainsKey("ShowHelpOnStartup") == false)
            {
                Application.Current.Properties["ShowHelpOnStartup"] = true;
            }
            if (Application.Current.Properties.ContainsKey("ShowCompletedRaces") == false)
            {
                Application.Current.Properties["ShowCompletedRaces"] = false;
            }
            //MainPage = new NavigationPage(new ResultsProcess());

            //MainPage = new NavigationPage(new Scanning());
            //If the token is present and they are remembered
            if ((Application.Current.Properties.ContainsKey("Token")) && (Application.Current.Properties.ContainsKey("IsRemembered")))
            {
                //And is blank/null
                if (((string)Application.Current.Properties["Token"] == "") || ((GlobalFunctions.PropertyToBool(Application.Current.Properties["IsRemembered"].ToString()) != true)))
                {
                    //They need to login
                    MainPage = new NavigationPage(new Login());
                }
                //Otherwise they can go straight to their page
                else
                {
                    if (GlobalFunctions.PropertyToBool(Application.Current.Properties["ShowHelpOnStartup"].ToString()) == true)
                    {
                        MainPage = new StartupHelp(true);
                    }
                    else
                    {
                        MainPage = new MainMenuMasterDetail();
                    }

                }
            }
            else
            {
                MainPage = new NavigationPage(new Login());
            }
        }

        public static TechsportiseData Database
        {
            get
            {
                if (database == null)
                {
                    database = new TechsportiseData(DependencyService.Get<IFileHelper>().GetLocalFilePath("Techsportise.db3"));
                }
                return database;
            }
        }


        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

界面(共享资产项目)

using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using Xamarin.Forms;

namespace TechsportiseApp.Helpers
{
    public class GlobalFunctions
    {
        public GlobalFunctions()
        {
        }

        public interface IFileHelper
        {
            string GetLocalFilePath(string filename);
        }

        public static bool CheckForInternetConnection()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
                request.Timeout = 5000;
                request.Credentials = CredentialCache.DefaultNetworkCredentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                    return true;
                else
                    //return false;
                    return true;
            }
            catch
            {
                //return false;
                return true;
            }

        }

        public static bool HasPremium()
        {
            var handler = new JwtSecurityTokenHandler();
            var token = handler.ReadToken(Application.Current.Properties["Token"].ToString()) as JwtSecurityToken;
            var premium = token.Claims.FirstOrDefault(claim => claim.Type == "premium").Value.ToString();
            var premiumbool = PropertyToBool(premium);

            return premiumbool;

        }
    }
}

平台类(本例中为Droid)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using TechsportiseApp.Droid;
using static TechsportiseApp.Helpers.GlobalFunctions;
using System.IO;

[assembly: Dependency(typeof(FileHelper))]
namespace TechsportiseApp.Droid
{
    public class FileHelper : IFileHelper
    {
        public string GetLocalFilePath(string filename)
        {
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            return Path.Combine(path, filename);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的数据库类不一致。初始化在实例构造函数中完成,而数据库调用是静态的,即使实例尚未初始化,也会引导您调用静态函数。

将函数更改为实例1(从函数中删除static限定符)或在静态构造函数上初始化数据库类。