【发布时间】:2020-07-19 03:23:52
【问题描述】:
我在 Visual Studio 中使用 Xamarin.Forms,并且我有一个我在表格中插入的产品列表。当我尝试更新 Cantidad、Precio 或其他功能时,Articulo 不会更新,并且列表会像以前一样显示。我正在尝试将 PK 用作字符串,因此当我尝试更新项目时,我需要 Articulo 的 Producto,它会验证 Producto 的 Id 是否存在于数据库中,然后进行更新。
这是我的模型 Articulo.cs
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace Saansa.Modelos
{
public class Articulo
{
[PrimaryKey,AutoIncrement]
public string Id { get; set; }
public string Producto { get; set; }
public int Precio { get; set; }
public int Cantidad { get; set; }
public string MasterCategory { get; set; }
public string Category1 { get; set; }
public string Category2 { get; set; }
public string Category3 { get; set; }
}
}
这是我的 xalm 文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Saansa"
x:Class="Saansa.Inventario"
Title="Inventario">
<StackLayout BackgroundColor="#f5cda2">
<Image Margin="0,0,0,0" HeightRequest="10" Source="inventario.png" ></Image>
<Label Margin="0,0,0,0" Text="Mi Cafe Delicias" FontAttributes="Bold"
FontSize="Large" TextColor="#b27b4b" HorizontalTextAlignment="Center" ></Label>
<Entry x:Name="txtProducto" Placeholder="Código Producto"></Entry>
<Entry x:Name="txtNombre" Placeholder="Nombre del producto"></Entry>
<Entry x:Name="txtPrecio" Placeholder="Precio del producto"></Entry>
<Entry x:Name="txtCantidad" Placeholder="Cantidad del Producto"></Entry>
<Entry x:Name="txtMainCategory" Placeholder="Categoria general"></Entry>
<Entry x:Name="txtSub1" Placeholder="Subcategoria 1"></Entry>
<Entry x:Name="txtSub2" Placeholder="Subcategoria 2"></Entry>
<Entry x:Name="txtSub3" Placeholder="Subcategoria 3"></Entry>
<StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
<Button x:Name="btnAdd" WidthRequest="200" Text="Añadir" Clicked="BtnAdd_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
<Button x:Name="btnRead" WidthRequest="200" Text="Buscar" Clicked="BtnRead_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
</StackLayout>
<StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
<Button x:Name="btnUpdate" WidthRequest="200" Text="Actualizar" Clicked="BtnUpdate_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
<Button x:Name="btnDelete" WidthRequest="200" Text="Borrar" Clicked="BtnDelete_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
</StackLayout>
<ListView x:Name="lstArticulo" BackgroundColor="#f5cda2">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Producto}"/>
<Label Text="{Binding Id}" HorizontalOptions="EndAndExpand"
TextColor="Black"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
在我的 Inventario.xalm.cs 中,我的更新选项是这样的:
private async void BtnUpdate_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtProducto.Text))
{
Modelos.Articulo articulo = new Modelos.Articulo()
{
Id = txtProducto.Text,
Producto = txtNombre.Text,
Precio = Convert.ToInt32(txtPrecio.Text),
Cantidad = Convert.ToInt32(txtCantidad.Text),
MasterCategory = txtMainCategory.Text,
Category1 = txtSub1.Text,
Category2 = txtSub2.Text,
Category3 = txtSub3.Text
};
//Update Person
await App.SQLiteDb.SaveItemAsync(articulo);
txtNombre.Text = string.Empty;
txtCantidad.Text = string.Empty;
txtProducto.Text = string.Empty;
txtPrecio.Text = string.Empty;
txtMainCategory.Text = string.Empty;
txtSub1.Text = string.Empty;
txtSub2.Text = string.Empty;
txtSub3.Text = string.Empty;
await DisplayAlert("Success", "Person Updated Successfully", "OK");
//Get All Persons
var articuloLista = await App.SQLiteDb.GetItemsAsync();
if (articuloLista != null)
{
lstArticulo.ItemsSource = articuloLista;
}
}
else
{
await DisplayAlert("Required", "Please Enter Nombre articulo", "OK");
}
}
还有我的 SQLiteHekper.cs:
public Task<int> SaveItemAsync(Modelos.Articulo articulo)
{ //articulo.Cantidad. !string.IsNullOrEmpty(articulo.Cantidad)
if (!string.IsNullOrEmpty(articulo.Id))
{
//probando esto
//var nuevoArticulo = GetItemAsync(articulo.Producto);
return db.UpdateAsync(articulo);
}
else
{
return db.InsertAsync(articulo);
}
}
【问题讨论】:
-
db.UpdateAsync(...)的返回值是多少? -
它必须是对象的列,具有我刚刚放置的新值,除了它的PK,但目前返回值不起作用,就像函数不更新列一样.
-
它应该返回更新的行数。如果它返回 0,那么你传递的对象有问题
-
[PrimaryKey,AutoIncrement] public string Id { get;放;字符串的自动增量将不起作用。你应该得到 sql 错误。
-
这并没有给我一个错误,但我理解你为什么这么说,问题是它没有搜索表并找到 PK。我将 PK 更改为 NotNull 并且它也不起作用。我尝试使用本教程:link 但在这里他们使用 PK 作为 int,我尝试将其用作字符串。那是我的问题
标签: c# sqlite xamarin.forms