数据未使用sqlite C#Xamarin表单保存到数据库

时间:2018-09-10 13:39:03

标签: c# sqlite xamarin

我正在尝试保存到SQLite数据库中,但未将其写入其中。调试时,条目的text属性设置为在运行时输入的文本,但是保存到数据库后,这些值设置为null,并且什么也不返回给ListView。下面是我的数据库模型:

  public class Products : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string  _Itemname;
    private string _Details;


    [PrimaryKey,AutoIncrement]
    public int ProductId { get; set; }
    [MaxLength(30)] [NotNull]
    public string Itemname {
        get
        {
            return _Itemname; 
        }
        set
        {
            if(_Itemname ==  value)
            return;

            _Itemname = value;

        }
    }

    public string ItemBrand { get; set; }

    [NotNull]
    public string Date { get; set; }
    public string Details {
        get
        {
            return _Details;
        }
        set
        {
            if (_Details == value)
                return;
            _Details = value;
        }
    }

    public string Category { get; set; }
    [NotNull]
    public string Supplier { get; set; }
    [NotNull]
    public string Price { get; set; }
    public string Quantity { get; set; }

    private void OnPropertyChanged([CallerMemberName]string propertyname=null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }

以下代码是我的addButton c#代码:       MyConnection = DependencyService.Get()。connection();

        //CreateTable();
        BindingContext = new Products
        {
            Itemname = ItemNameTextBox.Text,
            ItemBrand = ItemBrandTextBox.Text,
            Date = DateTextBox.ToString(),
            Details = DetailsTextBox.Text,
            Category = CategoryTextBox.ToString(),
            Supplier = SupplierTextBox.Text,
            Price = PriceTextBox.Text,
            Quantity = QuantityTextBox.Text
        };
    }

    private async void SaveItemButton_Clicked(object sender, EventArgs e)
    {
        var a = BindingContext as Products;
        if(string.IsNullOrWhiteSpace(ItemNameTextBox.Text)|| string.IsNullOrWhiteSpace(ItemBrandTextBox.Text)|| string.IsNullOrWhiteSpace(SupplierTextBox.Text)
            || string.IsNullOrWhiteSpace(PriceTextBox.Text) || string.IsNullOrWhiteSpace(QuantityTextBox.Text))
        {
           await DisplayAlert("Invalid Command", "Fields cannot be empty", "Ok");
            return;
        }
        else
        {
            await MyConnection.InsertAsync(a);
            ProductAdded?.Invoke(this, a);

            await DisplayAlert("Yay!!!", "Product added successfully....", "Ok");
           var result= await  DisplayAlert("Additional Prompt", "Do you want to add another product?", "Yes", "No");

            if (result) {               ItemBrandTextBox.Text = "";
            ItemNameTextBox.Text = "";
            DetailsTextBox.Text = "";
            SupplierTextBox.Text = "";
            PriceTextBox.Text = "";
            QuantityTextBox.Text = "";}
            else
            {
             await   Navigation.PopAsync();
            }

0 个答案:

没有答案