我正在开发一款需要简单的sqlite数据库的iOS应用。这是在使用Xamarin的便携式类库中。在代码中我试图获得与数据库的连接,但是我不确定我应该将数据库放在我的项目文件夹中,也不确定#if __ IOS__是否正常工作,但我使用的是关于Xamarin文档:http://bit.ly/1MxSYey
public static SQLiteConnection GetConnection()
{
#if __IOS__
var sqliteFilename = "messages.db";
var docs = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var db = Path.Combine(docs, "..", "Library", sqliteFilename);
return new SQLiteConnection(db);
#endif
return null;
}
答案 0 :(得分:1)
在PCL中,您应该使用接口和依赖注入,而不是像在共享解决方案中那样使用IF指令。
EG。 Xamarin Forms具有依赖注入内置(但您也可以使用另一个库):
PCL共享库:
public interface ISqlite {
SQLiteConnection GetConnection();
}
iOS特定项目:
[assembly: Dependency (typeof (SqliteApple))]
public class SqliteApple : ISqlite
{
public SQLite.SQLiteConnection GetConnection ()
{
var sqliteFilename = "messages.db";
var docs = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var db = Path.Combine(docs, "..", "Library", sqliteFilename);
return new SQLiteConnection(db);
}
}
然后像这样使用它:
var database = DependencyService.Get<ISqlite>().GetConnection();