我一直在尝试使用db访问的一个非常简单的xamarin表单应用程序教程,我在这里挣扎。
我使用VS 2017和Xamarin Forms with NET Standard。 另外,sqlite-net-pcl 1.4.118这是最新的稳定版。
这是我的MainActivity代码(android)
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
string fileName = "books_db.sqlite";
string fileLocation = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string fullPath = Path.Combine(fileLocation, fileName);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App(fullPath));
}
这是我的App.xaml(在共享库中)
/// <summary> Full pathname of the database file, sent through Android/iOS projects. </summary>
public static string DB_PATH = string.Empty;
public App(string DB_Path)
{
InitializeComponent();
DB_PATH = DB_Path;
MainPage = new NavigationPage(new MainPage());
}
这是我的MainPage(在共享库中)
protected override void OnAppearing()
{
base.OnAppearing();
try
{
// >> This throws an exception <<
using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
{
conn.CreateTable<Book>();
var books = conn.Table<Book>().ToList();
lviewBooks.ItemsSource = books;
}
}
catch (Exception ex)
{
DisplayAlert("Error", ex.GetType().ToString() + ex.Message, "OK");
}
}
一旦进入sqlite using语句,在我的模拟器(Android 4.4 API 19)中进行调试时会抛出异常。