我使用 nuget 包管理器下载 this package
示例显示:
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。
我什至在 source code 中找到了它,但我无法到达 .Platform
命名空间,因此我无法创建 SQLitePlatformIOS
对象。
using SQLite.Net; //.Platform.XamarinIOS <-- Compiler error if I try to reach this
我用来引用它的项目是一个可移植类项目,因为在引用文件夹中我看到了 .NET Portable Subset。
项目的结构是
iOS MainProject 引用示例可移植项目 示例可移植项目引用 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);
}
最佳答案
您找不到对 iOS 实现的引用的原因是您无权访问 PCL 中的平台特定代码。事实上,你并不需要它。 PCL 内部所需的只是接口(interface),特定代码将使用 DI 框架注入(inject),因此 PCL 为每个平台使用正确的代码,而无需在运行时检查您使用的是哪个平台。
Here你可以看到一个关于依赖注入(inject)如何工作的例子(它甚至使用 SQLite)
关于c# - 在 Sqlite.net PCL 中找不到 SQLitePlatformIOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653348/