2016-11-22 63 views
0

我似乎無法通過SQLite.Net-PCL下載死機中心;我有一個帶有PCL proj,Droid proj,iOS proj和Windows Universal項目的Xamarin表單解決方案。我已經安裝了SQLite.Net-PCL和SQLite.Net.Core-PCL的nuget軟件包。在Xamarin Forms中找不到SQLite.Net-PCL的工作示例

所以,我回到github項目找到一些很棒的例子來開始,他們都沒有工作;顯然你必須將一個平臺傳遞到數據庫連接,但我得到一個參考錯誤(如SQLitePlatformWin32)。

在網上搜索參考文獻...沒什麼。搜索平臺的nuget和...什麼都不是。

我錯過了什麼? (是的,我覺得啞)

他們有一個例子是

public class Database : SQLiteConnection 
{ 
    public Database(string path) : base(new SQLitePlatformWin32(), path) 
    { 
     CreateTable<Stock>(); 
     CreateTable<Valuation>(); 
    }} 

,我得到的是我不能在「新SQLitePlatformWin32」行解決引用錯誤。

+0

看看ToDo示例:https://github.com/xamarin/xamarin-forms-samples/tree/master/Todo – Jason

回答

1

對於任何掙扎的人,這裏是你需要做的。

  • 安裝SQLite.Net-PCL,SQLite.Net.Core-PCL和SQLite.Net.Async-PCL nugets
  • 在PCL創建ISQLite接口:

    public interface ISQLite { SQLiteConnection GetConnection(); }

  • 通過DependencyService

    PatientDatabase = DependencyService.Get<ISQLite>().GetConnection(); PatientDatabase.CreateTable<Patient>();

  • 呼叫的getConnection 0

  • 以上將根據您的平臺創建連接(即, android,ios)。在每個平臺的項目,你必須有一個的getConnection是通過DependencyService accessable像這樣的Android

    [assembly: Xamarin.Forms.Dependency(typeof(SQLite_Android))] // ... public class SQLite_Android : ISQLite { public SQLite_Android() { } public SQLite.Net.SQLiteConnection GetConnection() { var sqliteFilename = "TodoSQLite.db3"; string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder var path = Path.Combine(documentsPath, sqliteFilename); // Create the connection var conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), path); // Return the database connection return conn; } }

我的問題是我試圖創建在我的PCL的連接,我不能找到平臺,你需要做的是在每個單獨的項目中創建連接。

相關問題