简体   繁体   English

在Sqlite.net PCL中找不到SQLitePlatformIOS

[英]Can't find SQLitePlatformIOS in Sqlite.net PCL

I've used nuget package manager to download this package 我用nuget包管理器下载了这个包

The example shows: 该示例显示:

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);
    }
}

The issue I am facing is that I can't reference SQLitePlatformIOS. 我面临的问题是我无法引用SQLitePlatformIOS。 I even found it in the source code , but I'm not able to reach the .Platform namespace, thus I'm unable to create a SQLitePlatformIOS object. 我甚至在源代码中找到它,但是我无法访问.Platform命名空间,因此我无法创建SQLitePlatformIOS对象。

 using SQLite.Net; //.Platform.XamarinIOS <-- Compiler error if I try to reach this

The project I'm using to reference this is a portable class project, because in the references folder I see .NET Portable Subset. 我用来引用它的项目是一个可移植的类项目,因为在references文件夹中我看到了.NET Portable Subset。

The structure of the project is 该项目的结构是

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

In the Portable Project I have 在我的便携式项目中

References
     .NET Portable Subset
     From Packages
          SQLite.Net
Packages
     SQLite.Net-PCL
Data
     Repositories
           ExampleRepository.cs

My example repository is 我的示例存储库是

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>();
        }
    }
}

And I call this class from the iOS project. 我从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);
}

The reason you're not finding the reference to the iOS implementation is because you don't have access to platform specific code in a PCL. 您没有找到iOS实现的引用的原因是您无法访问PCL中的平台特定代码。 In fact, you don't need it. 事实上,你不需要它。 All you need inside the PCL is the interface, the specific code will be injected using a DI framework, so the PCL uses the proper code for each platform without you having to check which platform you're using during runtime. PCL内部所需的只是接口,特定代码将使用DI框架注入,因此PCL为每个平台使用正确的代码,而无需检查您在运行时使用的平台。

Here you can see an example on how dependency injection works (and it even uses SQLite) 在这里你可以看到一个关于依赖注入如何工作的例子(它甚至使用SQLite)

Yes, the .Platform namespace has disappeared from the SQLite.Net PCL package. 是的,.Platform命名空间已从SQLite.Net PCL包中消失。

The workaround is to download the source from https://github.com/oysteinkrog/SQLite.Net-PCL and then build the binaries yourself. 解决方法是从https://github.com/oysteinkrog/SQLite.Net-PCL下载源代码,然后自己构建二进制文件。 To do this, open the solution in Xamarin Studio (as of typing this I'm on 5.10.3 Build 51), change the mode to Release (without targeting any specific hardware), and click Build -> Build All . 为此,在Xamarin Studio中打开解决方案(在键入此项时,我在5.10.3 Build 51上),将模式更改为Release(不针对任何特定硬件),然后单击Build -> Build All

Then open your iOS project in Xamarin Studio, right-click on the References folder and select Edit References... , then click on the .Net Assembly tab, click Browse... and browse to the location of the SQLite.Net.Platform.XamarinIOS.Unified.dll file which you've just built. 然后在Xamarin Studio中打开您的iOS项目,右键单击References文件夹并选择Edit References... ,然后单击.Net Assembly选项卡,单击Browse...并浏览到SQLite.Net.Platform的位置你刚刚构建的.XamarinIOS.Unified.dll文件。 It should be in SQLite.Net-PCL-master/SQLite.Net.Platform.XamarinIOS.Unified/bin/Release 它应该在SQLite.Net-PCL-master/SQLite.Net.Platform.XamarinIOS.Unified/bin/Release

Although an old post from 2014, I found the SQLite.Net PCL GitHub link from https://forums.xamarin.com/discussion/28916/error-while-trying-to-install-sqlite-net-platform-xamarinios-2-4-1-please-help (See JuhaPehkonen's Dec 2014 post) 虽然是2014年的一篇旧文章,但我从https://forums.xamarin.com/discussion/28916/error-while-trying-to-install-sqlite-net-platform-xamarinios-2找到了SQLite.Net PCL GitHub链接。 -4-1-please-help (参见JuhaPehkonen 2014年12月的帖子)

And a bit more info on adding references in Xamarin Studio can be found here: How to add libraries to Xamarin References lists 有关在Xamarin Studio中添加引用的更多信息,请参见: 如何将库添加到Xamarin References列表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM