我使用NuGet包管理器下载了这个包。
示例显示:
我遇到的问题是无法引用SQLitePlatformIOS。 我甚至在源代码中找到了它,但我无法访问
示例显示:
public class SQLiteFactoryiOS : ISQLiteFactory
{
public SQLite.Net.SQLiteConnection CreateConnection(string dbName)
{
var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), dbName);
return new SQLite.Net.SQLiteConnection(new SQLitePlatformIOS(), path);
}
}
我遇到的问题是无法引用SQLitePlatformIOS。 我甚至在源代码中找到了它,但我无法访问
.Platform
命名空间,因此无法创建SQLitePlatformIOS
对象。 using SQLite.Net; //.Platform.XamarinIOS <-- Compiler error if I try to reach this
我参考的项目是一个可移植类项目,因为在引用文件夹中有.NET Portable Subset。
项目结构如下:
iOS主项目引用示例可移植项目,示例可移植项目引用SQLite.Net-PCL。
在可移植项目中:
References
.NET Portable Subset
From Packages
SQLite.Net
Packages
SQLite.Net-PCL
Data
Repositories
ExampleRepository.cs
我的示例仓库是
using System;
using Example.Data.DTO;
using Example.Models.Properties;
using SQLite.Net;
namespace Example.Data.Repositories
{
public class ExampleRepository(string dbPath)
{
using (var db = new SQLite.Net.SQLiteConnection(new SQLitePlatformIOS() /* Can't reference */, dbPath))
{
db.CreateTable<ExampleDTO>();
}
}
}
我会从iOS项目中调用这个类。
partial void SaveObject(NSObject sender)
{
var objectToSave = CreateObjectFromView();
var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Test.db3");
var repo = new ExampleRepository(dbPath);
}