我使用nuget软件包管理器下载这个包裹
这个例子显示:
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我用来引用的项目是一个可移植的类项目,因为在references文件夹中我看到了.NET可移植子集。
这个项目的结构是
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);
}发布于 2015-09-18 14:27:17
您之所以找不到对iOS实现的引用,是因为您无法访问PCL中特定于平台的代码。事实上,你不需要它。PCL内部所需要的只是接口,特定的代码将使用DI框架注入,因此PCL为每个平台使用适当的代码,而不必检查运行时使用的是哪个平台。
这里您可以看到一个关于依赖注入如何工作的示例(它甚至使用SQLite)。
发布于 2016-04-17 16:47:05
是的,.Platform命名空间已经从SQLite.Net PCL包中消失了。
解决方法是从https://github.com/oysteinkrog/SQLite.Net-PCL下载源代码,然后自己构建二进制文件。为此,在Xamarin中打开解决方案(在输入这个时,我正在5.10.3构建51上),将模式更改为发布(不针对任何特定的硬件),然后单击Build -> Build All。
然后在Xamarin中打开您的iOS项目,右键单击References文件夹并选择Edit References...,然后单击.Net Assembly选项卡,单击Browse...并浏览到您刚刚构建的SQLite.Net.Platform.XamarinIOS.Unified.dll文件的位置。应该在SQLite.Net-PCL-master/SQLite.Net.Platform.XamarinIOS.Unified/bin/Release里
虽然是2014年的一篇旧文章,但我找到了来自SQLite.Net GitHub的https://forums.xamarin.com/discussion/28916/error-while-trying-to-install-sqlite-net-platform-xamarinios-2-4-1-please-help链接(见JuhaPehkonen 2014年12月的帖子)。
关于在Xamarin中添加引用的更多信息,可以在这里找到:如何将库添加到Xamarin引用列表中
https://stackoverflow.com/questions/32653348
复制相似问题