Xamarin表格 - 使用SQLite
问题描述:
工作
我一直在努力遵循此视频以了解在Xamarin形式与SQLite的工作,我坚持Xamarin表格 - 使用SQLite
https://www.youtube.com/watch?v=FVH-9zjRP9M
project.Droid
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);
}
这是给我未处理的异常,当e xecuting
LocalFilePath = DependencyService.Get<Classes.ILocalDatabaseHelper>().GetLocalFilePath("TaskReminder.db3");
请大家帮忙。提前致谢。
NOTE(详细信息):
项目(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);
}
}
答
仔细一看,周围29 minutes and 10 seconds。你会看到他向项目类添加了一个属性。这在Xamarin.Forms中注册了依赖关系Dependency Service。
您可能忘了这么做,这就是为什么当您尝试解析接口的实现时,您会得到一个NullReferenceException
。
请查看Dependency Service的工作情况,了解这些类型的工作是如何工作的,并为自己解决这些错误,而不是仅仅关注视频。
+0
yup。意识到...谢谢! =) –
什么是例外?你对LocalDatabaseHelper的装饰如何? –
发生未处理的异常。发生了 –
你的LocalDatabaseHelper中是否有正确的DependencyService属性? – Jason