我試圖瞭解所有連接如何,我需要一些幫助。 到目前爲止,我可以做插入,更新,刪除到sqlite數據庫,但我不能讓UI顯示從數據庫的更改自動沒有我更新ListView上的ItemsSource每當對數據庫進行更改。即:ListView不會更新到Windows 10的UWP上的ObservableCollection中的新插入項目
在我App.xaml.cs
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
//Connection to the database and create the table if not there
string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
SQLiteConnection conn = new SQLiteConnection(path);
conn.CreateTable<CheckListItemModel>();
}
比我MainPage.xaml中我有兩個按鈕,一個簡單的頁面和列表視圖
<Page
x:Class="Personal_Checklist_2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Personal_Checklist_2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" >
<Button Content="Add Sample To Db" Click="Add_Sample_To_Db_Click" Margin="10,0,10,0" />
<Button Content="Clear Tasks" Click="Clear_Tasks_Click" />
</StackPanel>
<ListView Name="lvListView" Grid.Row="1" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Id}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
在MainPage.xaml.cs代碼隱藏我也保持簡單,因爲我知道只是添加新行到表中並清除表
using Personal_Checklist_2.DataModels;
using System.IO;
using System.Linq;
using SQLite;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Collections.ObjectModel;
namespace Personal_Checklist_2
{
public sealed partial class MainPage : Page
{
static string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
ObservableCollection<CheckListItemModel> Tasks = new ObservableCollection<CheckListItemModel>();
public MainPage()
{
this.InitializeComponent();
using (var conn = new SQLiteConnection(path))
{
var query = conn.Table<CheckListItemModel>();
Tasks = new ObservableCollection<CheckListItemModel>(query.ToList());
lvTasksList.ItemsSource = Tasks.ToList(); //<<<--- If I dont do this, list is not updated
}
}
private void Add_Sample_To_Db_Click(object sender, RoutedEventArgs e)
{
var Task = new CheckListItemModel()
{
taskTitle = "Sample Task"
};
using (var conn = new SQLiteConnection(path))
{
conn.Insert(Task);
var query = conn.Table<CheckListItemModel>();
Tasks = new ObservableCollection<CheckListItemModel>(query.ToList());
lvTasksList.ItemsSource = Tasks.ToList(); //<<<--- If I dont do this, list is not updated
}
}
private void Clear_Tasks_Click(object sender, RoutedEventArgs e)
{
using (var conn = new SQLiteConnection(path))
{
conn.DeleteAll<CheckListItemModel>();
var query = conn.Table<CheckListItemModel>();
Tasks = new ObservableCollection<CheckListItemModel>(query.ToList());
lvTasksList.ItemsSource = Tasks.ToList(); //<<<--- If I dont do this, list is not updated
}
}
}
}
數據庫表的模型看起來像這樣CheckListItemModel.cs
using SQLite;
using System;
using System.ComponentModel;
namespace Personal_Checklist_2.DataModels
{
class CheckListItemModel : INotifyPropertyChanged
{
#region Private fields on DataModel
private string _taskTitle;
#endregion
#region Public properties on DataModel
[PrimaryKey, AutoIncrement]
public int taskId { get; set; }
public string taskTitle
{
get { return _taskTitle; }
set { _taskTitle = value; NotifyPropertyChanged("taskTitle"); }
}
#endregion
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
不過,這並不正確看我這個樣子。有沒有辦法實現這一點,而無需設置listview.ItemsSource每次在db.sqlite中的一些變化,以及如何?
這個解決方案非常簡單,只是不知道這是否會工作,當我要更新的東西,即列表和數據庫將得到更新。我會試着讓你知道。謝謝 – al1en
正如我看到你已經實現INotifyPropertyChanged witch通知如果一個項目內的值已經改變。我可以看到你已經正確實施它。另外,我強烈建議確保'conn.Insert(Task);'或'conn.Update(Task);'是成功的。你可以返回一個bool e.x. bool'result = conn.Insert(Task);' – Stamos