2016-11-22 62 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的连接,我不能找到平台,你需要做的是在每个单独的项目中创建连接。

相关问题