我有一个应用需要使用 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 版本”如何查找?
最佳答案
使用 .Net Standard 2.0
和 Xamarin.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;
}
关于ios - 添加 iPhone 对 sqlite DB 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520382/