3

我已按照此处的说明 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

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

提前致谢

4

2 回答 2

3

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

var asyncDb = new SQLiteAsyncConnection(() => GetConnection());
于 2014-09-20T16:43:14.433 回答
0

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;
        }
于 2015-05-11T01:52:39.430 回答