Android Xamarin表单-使用SQLite

Android Xamarin表单-使用SQLite,android,sqlite,xamarin,xamarin.android,xamarin.forms,Android,Sqlite,Xamarin,Xamarin.android,Xamarin.forms,我一直在尝试跟随这段视频学习在Xamarin表单中使用SQLite,我被卡住了 机器人计划 class LocalDatabaseHelper : Classes.ILocalDatabaseHelper { public string GetLocalFilePath(string fileName) { string docFolder = System.Environment.GetFolderPath(System.Environment.SpecialF

我一直在尝试跟随这段视频学习在Xamarin表单中使用SQLite,我被卡住了

机器人计划

class LocalDatabaseHelper : Classes.ILocalDatabaseHelper
{
    public string GetLocalFilePath(string fileName)
    {
        string docFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string libFolder = Path.Combine(docFolder, "..", "Library", "Databases");

        if (!Directory.Exists(libFolder))
        {
            Directory.CreateDirectory(libFolder);
        }
        return Path.Combine(libFolder, fileName);
    }
}
项目pcl

    public static Classes.TaskReminder.TaskReminderDatabaseOperation Database
    {
        get
        {
            if (database == null)
            {
                string LocalFilePath = "";
                if(Device.OS==TargetPlatform.Android)
                {
                    LocalFilePath = DependencyService.Get<Classes.ILocalDatabaseHelper>().GetLocalFilePath("TaskReminder.db3");
                }
                database = new Classes.TaskReminder.TaskReminderDatabaseOperation(LocalFilePath);
            }
            return database;
        }
    }

public interface ILocalDatabaseHelper
{
    string GetLocalFilePath(string fileName);
}
它在执行时给了我未处理的异常

LocalFilePath = DependencyService.Get<Classes.ILocalDatabaseHelper>().GetLocalFilePath("TaskReminder.db3");
请帮忙。提前谢谢

请注意更多信息:

项目PCL

public class TaskReminderDatabaseOperation
    {
        readonly SQLiteAsyncConnection database;
        public TaskReminderDatabaseOperation(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<TaskReminder>().Wait();
        }

        public Task<List<TaskReminder>> GetTaskRemindersAsync()
        {
            return database.Table<TaskReminder>().ToListAsync();
        }

        public Task<TaskReminder> GetTaskReminder(DateTime datetime)
        {
            return database.Table<TaskReminder>().Where(i => i.ReminderDateTime == datetime).FirstOrDefaultAsync();
        }

        public Task<int> SaveTaskReminder(TaskReminder taskReminder)
        {
            if (taskReminder.Id == 0)
            {
                return database.InsertAsync(taskReminder);
            }
            else
            {
                return database.UpdateAsync(taskReminder);
            }
        }

        public Task<int> DeleteTaskReminder(TaskReminder taskReminder)
        {
            return database.DeleteAsync(taskReminder);
        }
    }

仔细看一下视频,看看周围。您将看到他向项目类添加了一个属性。这将向Xamarin.Forms中的注册依赖项

您可能忘记了这样做,这就是为什么在尝试解析接口的实现时会出现NullReferenceException


请查看依赖项服务的工作原理,以了解这类事情是如何工作的,并自行解决这些错误,而不是只关注视频。

有什么例外?您对LocalDatabaseHelper的修饰如何?发生了未处理的异常。发生您的LocalDatabaseHelper中是否有正确的DependencyService属性?请使用try/catch捕捉异常,并准确找出导致异常的原因。@Jason它显示的是nullreferenceexception。。这是否意味着我的GetLocalFilePath返回null?或者这是否意味着系统找不到GetLocalFilePath方法?