2014-09-20 19 views
3

我按照這裏的說明http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ - 同步連接到SQLite數據庫。Xamarin.Forms使用SQLite.Net.Async

public SQLiteConnection GetConnection() 
{ 
    var dbFilname = "localDB.db3"; 
    string docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 
    var path = Path.Combine(docsPath, dbFilname); 

    var plat = new SQLitePlatformAndroid(); 
    var conn = new SQLiteConnection(plat, path); 
    return conn; 
} 

我想將其更改爲異步連接(SQLiteAsyncConnection),但無法使其正常工作。

據這裏的指令 - https://components.xamarin.com/gettingstarted/sqlite-net - 它只是需要路徑作爲參數

var conn = new SQLiteAsyncConnection(path); 

不起作用,該錯誤表示,預計該參數是:

的連接功能,一個TaskScheduler和TaskCreationOptions

我不知道該怎麼做,一直沒能找到任何工作的例子。

在此先感謝

回答

3

你可以簡單地重複使用getConnection方法你已經擁有並創造這樣的異步連接:

var asyncDb = new SQLiteAsyncConnection(() => GetConnection()); 
+1

謝謝 - 對於有同樣問題的任何人,這裏有一個解決方案(http://www.captechconsulting.com/b log/nicholas-cipollina/cross-platform-sqlite-support-%E2%80%93-part-1)也可以運行 – user1667474 2014-09-21 00:21:15

+2

死鏈接請刪除或更改, – peterincumbria 2017-03-12 09:19:26

0

Xamarin平臺Android的配置,iOS和WP basicaly等於

public SQLiteAsyncConnection GetConnectionAsync() 
     { 
      const string fileName = "MyDB.db3"; 
      var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
      var path = Path.Combine(documentsPath, fileName); 

      var platform = new SQLitePlatformAndroid(); 

      var param = new SQLiteConnectionString(path, false); 
      var connection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(platform, param)); 

      return connection; 
     } 
相關問題