因此,我使用“ SQLite的数据库浏览器”],将数据库外部 Xamarin表单作为示例,并将其放在资产文件中
并且我正在尝试学习如何将数据库连接到数据库,所以我做到了这一点:
XAML:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Behoerdensprache.Pages.FinanzAmt"> <ContentPage.Content> <StackLayout> <Label Text="Welcome to Xamarin.Forms!" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" x:Name="LabelControl"/> <Entry Placeholder="Search" x:Name="SearchEntry"/> <Button Text="Press me" Clicked="Button_Clicked"/> </StackLayout> </ContentPage.Content> </ContentPage>
C#ButtonCode:
在Mainactivity和AppDelegate中定义:private void Button_Clicked(object sender, EventArgs e) { SQLiteConnection conn = new SQLiteConnection(App.DataBaseLocation); conn.CreateTable<Wortschatz>(); var gets = conn.Table<Wortschatz>().Where(i => i.Wort == SearchEntry.Text).ToString(); LabelControl.Text = gets; conn.Close(); }
App.DataBaseLocation
在主要活动中:
string dbName = "BeamtenSprache.sqlite"; string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); string fullPath = Path.Combine(folderPath, dbName); LoadApplication(new App(fullPath));
在AppDelegate中:
我在App.cs中定义的这部分:string dbName = "wortschatz.sqlite"; string folderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", "Library"); string fullPath = Path.Combine(folderPath, dbName); LoadApplication(new App(fullPath));
new App(fullPath)
public static string DataBaseLocation = string.Empty; public App() { InitializeComponent(); MainPage = new NavigationPage(new MainPage()); } public App(string databaseLocation) { InitializeComponent(); MainPage = new NavigationPage(new MainPage()); DataBaseLocation = databaseLocation; }
但是当我运行该应用程序并按下按钮时,此显示为:
因此,我在Xamarin Forms之外使用“ SQLite的数据库浏览器”在Xamarin Forms外部创建了一个示例数据库,并将其放在Assets File中,我试图学习如何将数据库连接到它,所以我做到了:XAML: