【问题标题】:Xamarin.Forms using SQLite.Net.AsyncXamarin.Forms 使用 SQLite.Net.Async
【发布时间】:2014-09-20 04:26:23
【问题描述】:

我已按照http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ 此处的说明 - 同步连接到 SQLite 数据库。

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

我不知道该怎么做,也找不到任何有效的例子。

提前致谢

【问题讨论】:

    标签: sqlite asynchronous xamarin sqlite.net


    【解决方案1】:

    您可以简单地重复使用已有的 GetConnection 方法并像这样创建异步连接:

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

    【讨论】:

    【解决方案2】:

    Xamarin Platforms Android Config,iOS 和 WP 基本相等

    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;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      相关资源
      最近更新 更多