2017-07-27 35 views
0
工作

我一直在努力遵循此視頻以瞭解在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); 
     } 
    } 
+0

什麼是例外?你對LocalDatabaseHelper的裝飾如何? –

+0

發生未處理的異常。發生了 –

+0

你的LocalDatabaseHelper中是否有正確的DependencyService屬性? – Jason

回答

0

仔細一看,周圍29 minutes and 10 seconds。你會看到他向項目類添加了一個屬性。這在Xamarin.Forms中註冊了依賴關係Dependency Service

您可能忘了這麼做,這就是爲什麼當您嘗試解析接口的實現時,您會得到一個NullReferenceException

請查看Dependency Service的工作情況,瞭解這些類型的工作是如何工作的,併爲自己解決這些錯誤,而不是僅僅關注視頻。

+0

yup。意識到...謝謝! =) –