首页 文章

Xamarin表示UWP Sqlite连接,无法加载文件或程序集SQLite-net

提问于
浏览
0

我被困在连接sqlite与xamarin表单/ pcl / uwp应用程序 . 以下是我为数据库设置路径的代码

Android (以下作品完美没问题)

App1.globals.path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//App1.globals.path=> /data/user/0/com.companyname.App1/files
LoadApplication(new App1.App());

UWP (以下不起作用)

App1.globals.path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
//App1.globals.path => C:\Users\sami.akram\AppData\Local\Packages\8e3de984-9360-4549-a5cc-80071995402b_hy7qfqqm9rwga\LocalState
LoadApplication(new App1.App());

以下是 App1 (Portable) =>主应用程序中的代码

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace App1
{

    public class globals
    {
        public static string path = "test.db3";
    }

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            var dbPath = System.IO.Path.Combine(globals.path, "test.db3");
            var db = new SQLiteConnection(dbPath);
            SetMainPage();
        }
    }
}

我面临的错误是

无法加载文件或程序集'SQLite-net,Version = 1.4.118.0,Culture = neutral,PublicKeyToken = null' . 定位的程序集的清单定义与程序集引用不匹配

enter image description here

但是程序集版本不应该是任何问题,因为我已经为图像显示的所有项目安装了相同的sqllite . 注意我尝试使用.net 4.5和.net 4.6的应用程序,但结果相同 .

enter image description here

我试过清洁解决方案重建解决方案 . 什么都没有帮助,相同的错误无法加载程序集..

5 回答

  • 0

    "sqlite-net-pcl" NuGet包版本 1.4.118.0 存在问题,该版本仅影响引用Xamarin.Forms PCL 项目的UWP项目 .
    该错误已通过"sqlite-net-pcl"版本1.5.166-beta修复 .

    官方SQLite-net GitHub页面添加了几个问题:

  • 0

    看来这是sqlite-net的一个特殊问题 . 即使执行了xamarin的页面(See xamarin's implementation),它也会显示错误 .

    对我有用的是将sqlite-net-pcl更新为1.5.166-beta .

  • 3

    您必须使用特定于平台的 Windows.Storage API来确定数据库文件路径 . 您可以使用以下链接获取帮助

    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/#Windows_10_Universal_Windows_Platform_UWP

    以下是示例代码:

    https://developer.xamarin.com/samples/xamarin-forms/Todo/

  • 3

    您可能还需要确保安装了Sqlite.org提供的VS扩展 .

    基本上,您需要在UWP Apps上启用SQLite . SQLite核心引擎已包含在iOS和Android中,但不包含在Windows上 . 因此,您需要为Visual Studio安装SQLite扩展,它为数据库引擎提供预编译的二进制文件,并自动执行将所需文件包含在新项目中的任务 .

    有关如何在UWP的Xamarin.Forms项目中集成Sqlite包的步骤的详细文章可以找到here

  • 0

    我已经测试了您的代码并重现了您的问题 . 问题是您无权使用 Version=4.0.10.0 System.IO 访问可移植库中的Windows本地文件夹 .

    portable library System.IO version

    portable library System.IO version
    UWP client project System.IO version

    UWP client project System.IO version

    在xamarin形式中使用sqlite的方法取决于依赖服务 . 你可以参考以下内容 .

    public interface IFileHelper
    {
      string GetLocalFilePath(string filename);
    }
    

    Interface implementation

    using Windows.Storage;
    ...
    
    [assembly: Dependency(typeof(FileHelper))]
    namespace Todo.UWP
    {
        public class FileHelper : IFileHelper
        {
            public string GetLocalFilePath(string filename)
            {
                return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
            }
        }
    }
    

    usage

    var path = DependencyService.Get<IFileHelper>().GetLocalFilePath("TodoSQLite.db3")
    var db = new SQLiteConnection(path);
    

    有关更多信息,请参阅Local Databases .

相关问题