使用SQLite将项添加到GridViewItem

时间:2016-12-18 16:28:19

标签: c# sqlite uwp

我今天更新了代码。

这就像在手机中添加联系人一样,我尝试使用SQLite将新项目(从数据库)添加到gridViewItem。

没有错误,但没有结果,我的意思是什么都没发生。

请检查。

我的xaml代码:

<Page
x:Class="HNT_listView2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HNT_listView2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:HNT_listView2.Models"
mc:Ignorable="d">

<Grid Background= "Salmon" Margin="0,0,10,0"  >

    <GridView ItemsSource="{x:Bind MyContactList}" 

              RightTapped="GridViewItem_RightTapped"
              IsRightTapEnabled="True"
       IsItemClickEnabled = "True"
        ItemClick = "GridViewItem_Click" Name = "NameOf_ItemClick" Margin="0,10,10,234" >


        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Contact">


                <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">


                    <Image Width="100" Height="120" Source="{x:Bind Photo}" HorizontalAlignment="Center" 
                           Stretch="UniformToFill"/>
                    <StackPanel Orientation="Vertical">
                        <TextBlock FontSize="30" Text="{x:Bind Name}" HorizontalAlignment="Center"/>
                        <TextBlock FontSize="30" Text="{x:Bind Phone}" HorizontalAlignment="Center"/>
                    </StackPanel>
                </StackPanel>

            </DataTemplate>
        </GridView.ItemTemplate>

    </GridView>
    <Button x:Name="Add" Content="Add a contact" HorizontalAlignment="Left" Margin="57,579,0,0" VerticalAlignment="Top" 
            Height="29" Width="120" Click="Add_Click"/>
    <Button x:Name="Retrieve" Content="Refesh" HorizontalAlignment="Left" Margin="183,579,0,0" VerticalAlignment="Top" 
            Height="29" Width="120" Click="Refresh_Click"/>

    <TextBox x:Name="name_textBox" HorizontalAlignment="Left" Margin="183,411,0,0" TextWrapping="Wrap" 
             Text="" VerticalAlignment="Top" Height="0" Width="157">

    </TextBox>
    <TextBlock x:Name="name_textBlock" HorizontalAlignment="Left" Margin="10,427,0,0" 
               TextWrapping="Wrap" Text="Add name" VerticalAlignment="Top"/>


    <TextBox x:Name="phone_textBox" HorizontalAlignment="Left" Margin="183,527,0,0" TextWrapping="Wrap" 
             Text="" VerticalAlignment="Top" Height="0" Width="157">

    </TextBox>
    <TextBlock x:Name="phone_textBlock" HorizontalAlignment="Left" Margin="0,485,0,0" 
               TextWrapping="Wrap" Text="Add phone number" VerticalAlignment="Top"/>


    <TextBox x:Name="photo_textBox" HorizontalAlignment="Left" Margin="183,473,0,0" TextWrapping="Wrap" 
             Text="" VerticalAlignment="Top" Height="0" Width="157">

    </TextBox>
    <TextBlock x:Name="photo_textBlock" HorizontalAlignment="Left" Margin="10,539,0,0" 
               TextWrapping="Wrap" Text="Add photo" VerticalAlignment="Top"/>



</Grid>

我的MainPage.xaml.cs代码:

 public sealed partial class MainPage : Page
{
    private List<Contact> MyContactList;

    string path;
    SQLite.Net.SQLiteConnection connection;


    public MainPage()
    {
        this.InitializeComponent();
        MyContactList = ContactManager.GetContacts();

        path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
        connection = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
        var f = connection.CreateTable<Contact>();



    }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
 var s = connection.Insert(new HNT_listView2.Models.Contact
        {
            Name = name_textBox.Text,
            Phone = phone_textBox.Text,
            Photo = photo_textBox.Text

        });
        Debug.WriteLine("Added");
    }

    public void Refresh_Click(Object sender, RoutedEventArgs e)
    {
        var query = connection.Table<Contact>();
        string name = "";
        string phone = "";
        string photo = "";

        foreach (var x in query)
        {
            name = name + "" + x.Name;
            phone = phone + "" + x.Phone;
            photo = photo + "" + x.Photo;
        }
        HNT_listView2.Models.ContactManager.GetContacts().Add(new Contact { Name = "" + name,Phone=""+phone, Photo=""+photo });

}
        }

My Contact.cs(用于绑定数据):

public class Contact
{
    public string Name { get; set; }
    public string Photo { get; set; }
    public string Phone { get; set; }
}


public class ContactManager
{
    public static List<Contact> GetContacts()
    {
        var contact1 = new List<Contact>();
        contact1.Add(new Contact { Name = "Nguyen Van A", Phone = "0168111222", Photo = "Assets/1.jpg" });
        contact1.Add(new Contact { Name = "Tran Van B", Phone = " 0168333444", Photo = "Assets/2.jpg" });
        contact1.Add(new Contact { Name = "Le Van C", Phone = "0166555666", Photo = "Assets/3.jpg" });          
        return contact1;
    }
}

请帮忙!

任何回复将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:0)

List类尚未实现INotifyPropertyChanged接口。因此,当您在列表中添加新项目时,它不会更新UI。

您可以使用ObservableCollectionObservableCollection<Contact>)来替换它。

public class ContactManager
{
    public static ObservableCollection<Contact> GetContacts()
    {
        var contact1 = new ObservableCollection<Contact>();
        contact1.Add(new Contact { Name = "Nguyen Van A", Phone = "0168111222", Photo = "Assets/1.jpg" });
        contact1.Add(new Contact { Name = "Tran Van B", Phone = " 0168333444", Photo = "Assets/2.jpg" });
        contact1.Add(new Contact { Name = "Le Van C", Phone = "0166555666", Photo = "Assets/3.jpg" });
        return contact1;
    }
}
public sealed partial class MainPage : Page
{
    private ObservableCollection<Contact> MyContactList;
    private string path;
    private SQLite.Net.SQLiteConnection connection;

    public MainPage()
    {
        this.InitializeComponent();
        MyContactList = ContactManager.GetContacts();
        path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
        connection = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
        var f = connection.CreateTable<Contact>();
    }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        var s = connection.Insert(new HNT_listView2.Models.Contact
        {
            Name = name_textBox.Text,
            Phone = phone_textBox.Text,
            Photo = photo_textBox.Text
        });
        Debug.WriteLine("Added");
    }

    public void Refresh_Click(Object sender, RoutedEventArgs e)
    {
        var query = connection.Table<Contact>();
        string name = "";
        string phone = "";
        string photo = "";

        foreach (var x in query)
        {
            name = name + "" + x.Name;
            phone = phone + "" + x.Phone;
            photo = photo + "" + x.Photo;
        }
        MyContactList.Add(new Contact { Name = "" + name, Phone = "" + phone, Photo = "" + photo });
    }
}

答案 1 :(得分:0)

实际上不是你的列表类缺少的INotifyPropertyChanged接口,而是INotifyCollectionChanged - 接口(由Xavier所说的ObservableCollection实现)