winrt sql数据不通过mvvm显示 [英] winrt sql data not displaying via mvvm

查看:76
本文介绍了winrt sql数据不通过mvvm显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Customer
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Contact { get; set; }







public ObservableCollection<Customer> _customerlist { get; set; }
        public Customer CustomerToAdd { get; set; }
        /// <summary>
        /// Add Customer data to the table
        /// </summary>
        public RelayCommand AddCustomerCommand { get; set; }
        private async void addCustomer()
        {
            var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"data.db3");
            using (var db = new SQLite.SQLiteConnection(dbpath))
            {
                // Create the tables if they don't exist
                db.Insert(new Customer()
                {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                    
                });


                db.Commit();
                db.Dispose();
                db.Close();
                var line = new MessageDialog("Records Inserted");
                await line.ShowAsync();
            }
        }
        /// <summary>
        /// Delete Selected Customer data from the table
        /// </summary>
        public RelayCommand DeleteSelectedCustomerCommand { get; set; }
        private void deleteSelectedCustomer()
        {

        }
        /// <summary>
        /// edit Selected Customer data from the table
        /// </summary>
        public RelayCommand EditSelectedCustomerCommand { get; set; }
        private void editSelectedCustomer()
        {

        }
        /// <summary>
        /// Delete All Customer data from the table
        /// </summary>
        public RelayCommand DeleteAllCustomerCommand { get; set; }
        private void deleteAll()
        {

        }

        public MainPageViewModel()
        {
            AddCustomerCommand = new RelayCommand(addCustomer);
            CustomerToAdd = new Customer();
            _customerlist = new ObservableCollection<Customer>();
            DeleteAllCustomerCommand = new RelayCommand(deleteAll);
            EditSelectedCustomerCommand = new RelayCommand(editSelectedCustomer);
            DeleteSelectedCustomerCommand = new RelayCommand(deleteSelectedCustomer);
        }







<Page.DataContext>
        <ViewModels:MainPageViewModel>

        </ViewModels:MainPageViewModel>
    </Page.DataContext>

    <Grid removed="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding _customerlist}"

                  HorizontalAlignment="Left" Margin="44,65,0,327" Width="456">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="400" removed="Chocolate">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" FontSize="30" />
                            <TextBlock Text="," FontSize="30" />
                            <TextBlock Text="{Binding City }" FontSize="30" />
                        </StackPanel>
                        <TextBlock Text="{Binding Contact}" FontSize="30" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Button Command="{Binding  AddCustomerCommand}" 

        		Content="Add person" 

        		FontSize="40" Margin="588,465,0,230"/>
        <Button Command="{Binding EditSelectedCustomerCommand}" 

        		Content="Edit" 

        		FontSize="40" Margin="865,465,0,230"/>
        <Button Command="{Binding DeleteSelectedCustomerCommand}" 

                Content="Delete" 

                FontSize="40" Margin="1037,465,0,230" />
        <Button Command="{Binding DeleteAllCustomerCommand }" 

                Content="Delete All" 

                FontSize="40" Margin="979,619,0,76" />

        <TextBlock Text="Name" FontSize="30" Margin="633,65,598,640" Height="63"/>
        <TextBox DataContext="{Binding CustomerToAdd}" Text="{Binding Name, Mode=TwoWay}" 

                 FontSize="30" Margin="868,62,80,640"/>
        <TextBlock Text="City " FontSize="30" Margin="633,181,551,524"/>
        <TextBox DataContext="{Binding CustomerToAdd}" Text="{Binding City, Mode=TwoWay}" 

                 FontSize="30" Margin="868,181,80,525"/>
        <TextBlock Text="Contact" FontSize="30" Margin="633,296,536,400"/>
        <TextBox DataContext="{Binding CustomerToAdd}" Text="{Binding Contact, Mode=TwoWay}" 

                 FontSize="30" Margin="868,296,80,403"/>
    </Grid>





i我插入数据及其显示已插入记录但列表框未显示数据



i am insert data and its show "Records Inserted" but list box not showing that data

推荐答案

private async void addCustomer()
        {
            var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"data.db3");
            using (var db = new SQLite.SQLiteConnection(dbpath))
            {
                // Create the tables if they don't exist
                db.Insert(new Customer()
                {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                    
                });
                var newCustomer = new Customer()
                {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                };
                db.Insert(newCustomer);
                _Customerlist.Add(newCustomer);
               
                db.Commit();
                db.Dispose();
                db.Close();
                var line = new MessageDialog("Records Inserted");
                await line.ShowAsync();
            }
        }


试试这个

Try this
Instead of using this
  db.Insert(new Customer()
                {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                    
                });
 
Use the following
 Customer newCustomer = new Customer() {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                    
                };
db.Insert(newCustomer);
customerlist.Add(newCustomer);





原因是你在DB中插入recod但是没有添加同样的to the list which is binded to lsitbox.



so you either need to add the customer object to the list whenever a new object is created or you need to pull the data from DB.



The reason is you are inserting the recod in DB but the same is not added to the list which is binded to lsitbox.

so you either need to add the customer object to the list whenever a new object is created or you need to pull the data from DB.


这篇关于winrt sql数据不通过mvvm显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆