简体   繁体   中英

Xamarin forms stacklayout returning null

I tried to get the item on stacklayout into an SQLite Database, but it just won't carry any data with.

  private void MainCategory_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var carier = e.SelectedItem as Item;






        var cart_Itemsx = new List<cart_Items>();
        cart_Itemsx.Add(new Models.cart_Items { cartid = 1, Id = carier.itid, image = carier.image, name = carier.title, price = carier.price1, quantity = "1", type = "Wash and Iron" });
        cart_Itemsx.Add(new Models.cart_Items { cartid = 2, Id = carier.itid, image = carier.image, name = carier.title, price = carier.price2, quantity = "1", type = "Iron Only" });
        SubCategory.ItemsSource = cart_Itemsx.ToList();
    }



    private void SubCategory_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var dbcontet = e.SelectedItem as cart_Items;
        _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "WashPro.db3");
        var db = new SQLiteConnection(_dbPath);
        db.CreateTable<cart_Items>();



        var MaximumPrimaryKey = db.Table<cart_Items>().OrderByDescending(zt => zt.cartid).FirstOrDefault();

        var waltani = new cart_Items()
        {
            cartid = (MaximumPrimaryKey == null ? 1 : MaximumPrimaryKey.cartid + 1),
            Id = dbcontet.Id,
            image = dbcontet.image,
            name = dbcontet.name,
            price = dbcontet.price,
            quantity = dbcontet.quantity,
            type = dbcontet.quantity
        };

        if (MaximumPrimaryKey == null)
        {
            db.Insert(waltani);
        }
        else if (MaximumPrimaryKey != null)
        {
            var MaximumQuantityKey = db.Table<cart_Items>().Where(m => m.cartid.Equals(dbcontet.cartid) && m.type.Equals(dbcontet.type)).FirstOrDefault();

            if (MaximumQuantityKey != null)
            {
               waltani.price = dbcontet.price = 1;
                db.Update(waltani);
            }

        }
        SubCategory.SelectedItem = null;

    }

image of the null error I got

我得到的空错误的图像

I cannot even begin to understand the problem. The way I found around the problem will make my already dirty code way dirtier.

I have the damaging method I tried was using the selected context of the main stack panel to influence the second stack pannel.

I have even made the primary key of the cart_item model null.

Your selected element is null or database table does not contain any elements you are looking for in query, which is more likely because you are trying to initialize your database on SubCategory_ItemSelected. This is wrong approach.

Try to check if database item exist first.

var exist = db.Table<cart_Items>().OrderByDescending(zt => zt.cartid).Any();

if (exist)
{
        var MaximumPrimaryKey = db.Table<cart_Items>().OrderByDescending(zt => zt.cartid).FirstOrDefault();

        var waltani = new cart_Items()
        {
            cartid = (MaximumPrimaryKey == null ? 1 : MaximumPrimaryKey.cartid + 1),
            Id = dbcontet.Id,
            image = dbcontet.image,
            name = dbcontet.name,
            price = dbcontet.price,
            quantity = dbcontet.quantity,
            type = dbcontet.quantity
        };
}

The problem lies at the last line of code.

SubListview.SelectedItem = null;

This somehow makes the casting not see the e.selected items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM