在Sqlite.net PCL中找不到SQLitePlatformIOS

mac*_*688 3 c# sqlite xamarin.ios ios xamarin

我用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我面临的问题是我无法引用SQLitePlatformIOS.我甚至在源代码中找到了它,但是我无法访问.Platform命名空间,因此我无法创建SQLitePlatformIOS对象.

 using SQLite.Net; //.Platform.XamarinIOS <-- Compiler error if I try to reach this
Run Code Online (Sandbox Code Playgroud)

我用来引用它的项目是一个可移植的类项目,因为在references文件夹中我看到了.NET Portable Subset.

该项目的结构是

iOS MainProject引用示例可移植项目示例可移植项目引用SQLite.Net-PCL

在我的便携式项目中

References
     .NET Portable Subset
     From Packages
          SQLite.Net
Packages
     SQLite.Net-PCL
Data
     Repositories
           ExampleRepository.cs
Run Code Online (Sandbox Code Playgroud)

我的示例存储库是

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>();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我从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);
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*osa 7

您没有找到iOS实现的引用的原因是您无法访问PCL中的平台特定代码.事实上,你不需要它.PCL内部所需的只是接口,特定代码将使用DI框架注入,因此PCL为每个平台使用正确的代码,而无需检查您在运行时使用的平台.

在这里你可以看到一个关于依赖注入如何工作的例子(它甚至使用SQLite)