2017-08-10 73 views
0

我被困在與xamarin窗體/ pcl/uwp應用程序連接sqlite。以下是我的代碼來設置路徑數據庫Xamarin Forms UWP Sqlite連接,無法加載文件或程序集SQLite-net

的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的網,版本= 1.4.118.0,文化=中性公鑰=空'。在位於集清單定義不匹配的程序集引用

enter image description here

但正如我已經安裝了相同的sqllite爲以下圖片的所有項目顯示它裝配的版本應該不會有任何問題。注意我嘗試了與.net 4.5和.net 4.6的應用程序,但結果相同。

enter image description here

我試圖清理解決方案的改造解決方案。沒有什麼幫助的是,同樣的錯誤不能加載程序集..

+0

這是一個重複的問題。請參閱[這裏](https://stackoverflow.com/questions/45497992/pcl-vs-net-standard-library-for-sqlite-in-uwp) – PJTewkesbury

回答

0

你必須使用特定於平臺的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/

+0

對不起親愛的我編輯我的問題。當使用'ApplicationData.Current.LocalFolder.Path'時,它仍然會給出同樣的例外 – Sami

0

我已經測試你的代碼並再現您的問題。問題是,您沒有權限訪問便攜式庫中的Windows本地文件夾,其中Version=4.0.10.0System.IO

移植庫System.IO版本

portable library System.IO version UWP客戶端項目System.IO版本

UWP client project System.IO version

到xamarin表單中使用SQLite的方法取決於依存服務。你可以參考以下內容。

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

接口實現

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

使用

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

欲瞭解更多請參考Local Databases

0

看來這是sqlite-net的一個特殊問題。 即使執行了xamarin的頁面(See xamarin's implementation),它也顯示錯誤。

我的工作是更新sqlite-net-pcl到1.5.166-beta。

3

您可能還需要確保您已安裝Sqlite.org提供的VS擴展。

基本上,您需要在UWP應用上啓用SQLite。 SQLite核心引擎已經包含在iOS和Android上,但不包括在Windows上。因此,您需要安裝用於Visual Studio的SQLite擴展,該擴展爲數據庫引擎提供預編譯的二進制文件,並自動執行包含新項目所需文件的任務。

就如何SQLite的包Xamarin.Forms項目UWP整合步驟的詳細的文章,可以發現here

+1

謝謝先生。當我購買vs企業時,忘了再次將其添加到我的項目中。 –

相關問題