C# Windows通用应用程序-在Listview中显示SQLite数据
我已经尝试了一段时间,但是没有太多的代码。。我将向您展示我的Meds.xaml和Meds.xaml.cs代码,看看是否可以获得一些帮助,将数据绑定到Listview中。基本上,填写表单,单击按钮,将其添加到数据库并更新ListView Meds.xamlC# Windows通用应用程序-在Listview中显示SQLite数据,c#,sqlite,win-universal-app,C#,Sqlite,Win Universal App,我已经尝试了一段时间,但是没有太多的代码。。我将向您展示我的Meds.xaml和Meds.xaml.cs代码,看看是否可以获得一些帮助,将数据绑定到Listview中。基本上,填写表单,单击按钮,将其添加到数据库并更新ListView Meds.xaml <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Medication Name:" VerticalAlignment
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Medication Name:" VerticalAlignment="Top" Margin="43,255,0,0" Foreground="#FFC6C6C6" FontSize="18.667"/>
<TextBox x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="265,248,0,0" Width="186"/>
<TextBlock x:Name="textBlock1_Copy" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Medication Total Dose:" VerticalAlignment="Top"
看看我能否得到一些帮助,将数据绑定到Listview中。基本上,填写表单,单击按钮,将其添加到数据库并更新ListView
根据您的描述并基于您提供的上述代码,我制作了一个简单的示例,演示如何使用和SQLite.Net-PCL在ListView中显示SQLite数据,供您参考:
Meds.xaml:
<StackPanel>
<TextBox Header="Medication Name: " x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="Medication Total Dose: " x:Name="meddose_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="What For: " x:Name="whatfor_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click" Width="186" Margin="0,5" />
<Button x:Name="btnBack" Content="Back" Click="btnBack_Click" Width="186" Margin="0,5" />
<ListView Header="Medications" x:Name="myList" Background="White" HorizontalAlignment="Left" Width="400">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Bind data to ListView Item, here I just bind three items for Medications, you can add other items if needed-->
<TextBlock x:Name="medName" Text="{Binding Path=MedName}" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" x:Name="medDose" Text="{Binding Path=MedDose}" TextWrapping="Wrap" />
<TextBlock Grid.Column="2" x:Name="whatFor" Text="{Binding Path=WhatFor}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
public sealed partial class Meds : Page
{
private string DBPath { get; set; }
public Meds()
{
this.InitializeComponent();
// Create the Database
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
conn.CreateTable<Medications>();
// Set ItemsSource to the sqlite data for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
// Add Medications
conn.Insert(new Medications
{
MedName = medname_box.Text,
MedDose = meddose_box.Text,
WhatFor = whatfor_box.Text
});
// Update the ItemsSource for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
}
Meds.xaml.cs:
<StackPanel>
<TextBox Header="Medication Name: " x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="Medication Total Dose: " x:Name="meddose_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="What For: " x:Name="whatfor_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click" Width="186" Margin="0,5" />
<Button x:Name="btnBack" Content="Back" Click="btnBack_Click" Width="186" Margin="0,5" />
<ListView Header="Medications" x:Name="myList" Background="White" HorizontalAlignment="Left" Width="400">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Bind data to ListView Item, here I just bind three items for Medications, you can add other items if needed-->
<TextBlock x:Name="medName" Text="{Binding Path=MedName}" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" x:Name="medDose" Text="{Binding Path=MedDose}" TextWrapping="Wrap" />
<TextBlock Grid.Column="2" x:Name="whatFor" Text="{Binding Path=WhatFor}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
public sealed partial class Meds : Page
{
private string DBPath { get; set; }
public Meds()
{
this.InitializeComponent();
// Create the Database
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
conn.CreateTable<Medications>();
// Set ItemsSource to the sqlite data for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
// Add Medications
conn.Insert(new Medications
{
MedName = medname_box.Text,
MedDose = meddose_box.Text,
WhatFor = whatfor_box.Text
});
// Update the ItemsSource for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
}
公共密封部分类药物:第页
{
私有字符串DBPath{get;set;}
公共药物()
{
this.InitializeComponent();
//创建数据库
DBPath=Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,“meds.sqlite”);
使用(SQLite.Net.SQLiteConnection conn=new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),DBPath))
{
conn.CreateTable();
//将ItemsSource设置为ListView的sqlite数据
myList.ItemsSource=conn.Table();
}
}
私有无效btnAdd_单击(对象发送者,路由目标e)
{
使用(SQLite.Net.SQLiteConnection conn=new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),DBPath))
{
//添加药物
conn.Insert(新药)
{
MedName=MedName\u box.Text,
MedDose=MedDose\u box.Text,
WhatFor=WhatFor\u box.Text
});
//更新ListView的ItemsSource
myList.ItemsSource=conn.Table();
}
}
私有无效btnBack\u单击(对象发送方,路由目标)
{
if(框架CanGoBack)
{
Frame.GoBack();
}
}
}
以下是结果,以下是输出:
看看我能否得到一些帮助,将数据绑定到Listview中。基本上,填写表单,单击按钮,将其添加到数据库并更新ListView
根据您的描述并基于您提供的上述代码,我制作了一个简单的示例,演示如何使用和SQLite.Net-PCL在ListView中显示SQLite数据,供您参考:
Meds.xaml:
<StackPanel>
<TextBox Header="Medication Name: " x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="Medication Total Dose: " x:Name="meddose_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="What For: " x:Name="whatfor_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click" Width="186" Margin="0,5" />
<Button x:Name="btnBack" Content="Back" Click="btnBack_Click" Width="186" Margin="0,5" />
<ListView Header="Medications" x:Name="myList" Background="White" HorizontalAlignment="Left" Width="400">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Bind data to ListView Item, here I just bind three items for Medications, you can add other items if needed-->
<TextBlock x:Name="medName" Text="{Binding Path=MedName}" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" x:Name="medDose" Text="{Binding Path=MedDose}" TextWrapping="Wrap" />
<TextBlock Grid.Column="2" x:Name="whatFor" Text="{Binding Path=WhatFor}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
public sealed partial class Meds : Page
{
private string DBPath { get; set; }
public Meds()
{
this.InitializeComponent();
// Create the Database
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
conn.CreateTable<Medications>();
// Set ItemsSource to the sqlite data for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
// Add Medications
conn.Insert(new Medications
{
MedName = medname_box.Text,
MedDose = meddose_box.Text,
WhatFor = whatfor_box.Text
});
// Update the ItemsSource for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
}
Meds.xaml.cs:
<StackPanel>
<TextBox Header="Medication Name: " x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="Medication Total Dose: " x:Name="meddose_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="What For: " x:Name="whatfor_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click" Width="186" Margin="0,5" />
<Button x:Name="btnBack" Content="Back" Click="btnBack_Click" Width="186" Margin="0,5" />
<ListView Header="Medications" x:Name="myList" Background="White" HorizontalAlignment="Left" Width="400">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Bind data to ListView Item, here I just bind three items for Medications, you can add other items if needed-->
<TextBlock x:Name="medName" Text="{Binding Path=MedName}" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" x:Name="medDose" Text="{Binding Path=MedDose}" TextWrapping="Wrap" />
<TextBlock Grid.Column="2" x:Name="whatFor" Text="{Binding Path=WhatFor}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
public sealed partial class Meds : Page
{
private string DBPath { get; set; }
public Meds()
{
this.InitializeComponent();
// Create the Database
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
conn.CreateTable<Medications>();
// Set ItemsSource to the sqlite data for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
// Add Medications
conn.Insert(new Medications
{
MedName = medname_box.Text,
MedDose = meddose_box.Text,
WhatFor = whatfor_box.Text
});
// Update the ItemsSource for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
}
公共密封部分类药物:第页
{
私有字符串DBPath{get;set;}
公共药物()
{
this.InitializeComponent();
//创建数据库
DBPath=Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,“meds.sqlite”);
使用(SQLite.Net.SQLiteConnection conn=new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),DBPath))
{
conn.CreateTable();
//将ItemsSource设置为ListView的sqlite数据
myList.ItemsSource=conn.Table();
}
}
私有无效btnAdd_单击(对象发送者,路由目标e)
{
使用(SQLite.Net.SQLiteConnection conn=new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),DBPath))
{
//添加药物
conn.Insert(新药)
{
MedName=MedName\u box.Text,
MedDose=MedDose\u box.Text,
WhatFor=WhatFor\u box.Text
});
//更新ListView的ItemsSource
myList.ItemsSource=conn.Table();
}
}
私有无效btnBack\u单击(对象发送方,路由目标)
{
if(框架CanGoBack)
{
Frame.GoBack();
}
}
}
以下是结果,以下是输出:
你的问题是。。。?请更精确一点,删除不必要的代码,比如注释掉的东西。你的问题是。。。?请更精确一点,删除不必要的代码,比如注释掉的东西。