对不起,是否曾经有人问过这个问题,现在搜索了好几个小时都没用。
我正在使用sqlite-net-pcl nuget。根据此类创建了一个表:
public class ProductCategory
{
private int _id;
private string _prodCategory;
private string _prodSubCategory;
[PrimaryKey, AutoIncrement]
public int CategoryID { get { return _id; } set { _id = value; } }
public string ProdCategory { get { return _prodCategory; } set { _prodCategory = value; } }
public string ProdSubCategory { get { return _prodSubCategory; } set { _prodSubCategory = value; } }
public ProductCategory()
{
}
public ProductCategory(string category, string subcategory)
{
_prodCategory = category;
_prodSubCategory = subcategory;
}
}
我以这种方式创建表:
database.CreateTableAsync<ProductCategory>().Wait();
这是将此类的实例保存到表中的方式:
public Task<int> SaveCategoryAsync(ProductCategory category)
{
if (category.CategoryID == 0)
{
return database.InsertAsync(category);
}
else
{
return database.UpdateAsync(category);
}
}
当我尝试使用以下代码将表的内容作为列表检索时,我得到了System.InvalidCastException:指定的转换无效:
public async Task<ObservableCollection<ProductCategory>> GetProductCategoriesAsync()
{
ObservableCollection<ProductCategory> lst = new ObservableCollection<ProductCategory>(await database.Table<ProductCategory>().ToListAsync());
return lst;
}
这是用DBite for SQLite打开表时的外观:
CREATE TABLE "ProductCategory" ( "ID" integer primary key autoincrement not null , "ProdCategory" varchar , "ProdSubCategory" varchar )
这是控制台的输出。在那找不到有用的东西:
Loaded assembly: System.Xml.dll [External]
Loaded assembly: Anonymously Hosted DynamicMethods Assembly [External]
Loaded assembly: System.Threading.dll [External]
Loaded assembly: System.Runtime.Extensions.dll [External]
Loaded assembly: System.Globalization.dll [External]
Thread started: <Thread Pool> #2
Thread started: <Thread Pool> #3
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Loaded assembly: System.Linq.dll [External]
03-02 19:19:31.592 E/HW-JPEG-DEC( 8873): [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
03-02 19:19:31.696 I/HwSecImmHelper( 8873): mSecurityInputMethodService is null
03-02 19:19:31.701 I/HwPointEventFilter( 8873): do not support AFT because of no config
03-02 19:19:31.861 I/OpenGLRenderer( 8873): Initialized EGL, version 1.4
WARNING: linker: 03-02 19:19:31.871 W/linker ( 8873): /vendor/lib64/libhwuibp.so: unused DT entry: type 0xf arg 0xe3a
/vendor/lib64/libhwuibp.so: unused DT entry: type 0xf arg 0xe3a
03-02 19:19:32.047 I/BitmapFactory( 8873): set decoder allocator to gralloc
03-02 19:19:32.051 I/gralloc ( 8873): alloc_device_alloc:564: Alloc handle(0x7de66ed000): interfmt=1, stride=1024, size=6144000, usage=0x2000003
03-02 19:19:32.117 I/gralloc ( 8873): alloc_device_alloc:564: Alloc handle(0x7de66ed300): interfmt=1, stride=3072, size=55296000, usage=0x2000003
03-02 19:19:32.202 I/gralloc ( 8873): alloc_device_free:586: Free handle(0x7de66ed300)
03-02 19:19:32.203 I/gralloc ( 8873): alloc_device_free:586: Free handle(0x7de66ed000)
03-02 19:19:32.203 E/HW-JPEG-DEC( 8873): [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
03-02 19:19:32.309 W/art ( 8873): JNI RegisterNativeMethods: attempt to register 0 native methods for md58432a647068b097f9637064b8985a5e0.ViewRenderer_2
03-02 19:19:32.544 I/Choreographer( 8873): Skipped 30 frames! The application may be doing too much work on its main thread.
03-02 19:19:33.382 I/hwaps ( 8873): JNI_OnLoad
03-02 19:19:33.702 I/HwSecImmHelper( 8873): mSecurityInputMethodService is null
03-02 19:19:34.913 I/HwSecImmHelper( 8873): mSecurityInputMethodService is null
03-02 19:19:35.081 I/HwSecImmHelper( 8873): mSecurityInputMethodService is null
03-02 19:19:35.138 W/art ( 8873): JNI RegisterNativeMethods: attempt to register 0 native methods for md51558244f76c53b6aeda52c8a337f2c37.EntryRenderer
03-02 19:19:35.168 E/HW-JPEG-DEC( 8873): [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
03-02 19:19:35.169 E/HW-JPEG-DEC( 8873): [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
03-02 19:19:35.171 E/HW-JPEG-DEC( 8873): [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
Resolved pending breakpoint at 'ProductDB.cs:54,1' to void KetoMac.DB.ProductDB.<GetProductCategoriesAsync>d__3.MoveNext () [0x000fd].
Loaded assembly: System.Linq.Expressions.dll [External]
03-02 19:19:35.604 W/View ( 8873): requestLayout() improperly called by md51558244f76c53b6aeda52c8a337f2c37.ScrollViewContainer{34296db V.E...... ......ID 0,0-1080,1832} during layout: running second layout pass
03-02 19:19:35.736 W/View ( 8873): requestLayout() improperly called by md58432a647068b097f9637064b8985a5e0.NavigationPageRenderer{ac2000f V.E...... ......ID 0,0-1080,1760 #1} during second layout pass: posting in next frame
Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
答案 0 :(得分:0)
毕竟这是XAML错误。 <ListView>
标记中的元素在<DataTemplate></DataTemplate>
的标记内。在其中添加<ViewCell></ViewCell>
和标签即可解决此问题。感谢this线程中的JMK。