添加 IPhone 对 sqlite 数据库的支持

Adding IPhone support for sqlLite DB

我有一个应用程序需要使用 Xamarin IPhone。

这是它在我的 Android 构建中的样子:

using myMood.Data;
using myMood.Droid.Data;
using System.IO;
using Xamarin.Forms;

[assembly: Dependency(typeof(SQLite_Android))]
namespace myMood.Droid.Data
{
    public class SQLite_Android : ISQLite
    {
        public SQLite_Android() { }
        public SQLite.SQLiteConnection GetConnection()
        {
            var sqliteFileName = "myMood.db3";
            string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(documentsPath, sqliteFileName);
            var conn = new SQLite.SQLiteConnection(path);

            return conn;
        }
    }
}

"IPhone version" 会如何查找?

使用 .Net Standard 2.0Xamarin.Forms 您可以在您的共享代码中直接实现此连接对象

public static SQLiteAsyncConnection GetSQliteAsyncConnection()
    {
        string sqliteFileName = "myMood.db3";
        string dbPath = string.Empty;
        switch (Xamarin.Forms.Device.RuntimePlatform)
        {
            case Device.Android:
                string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                dbPath = Path.Combine(documentsPath, sqliteFileName);
                break;
            case Device.iOS:
                string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string libFolder = System.IO.Path.Combine(docFolder, "..", "Library");
                if (!System.IO.Directory.Exists(libFolder))
                {
                    System.IO.Directory.CreateDirectory(libFolder);
                }
                dbPath = System.IO.Path.Combine(libFolder, sqliteFileName);
                break;
            default:
                break;
        }
        var conn = new SQLiteAsyncConnection(dbPath);
        return conn;
    }