首页 > 解决方案 > 从 sqlite3 检索对象时“指定的转换无效”

问题描述

抱歉,如果以前有人问过这个问题,现在搜索了几个小时无济于事。

我正在使用 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: Specified cast is not valid:

public async Task<ObservableCollection<ProductCategory>> GetProductCategoriesAsync()
        {
            ObservableCollection<ProductCategory> lst = new ObservableCollection<ProductCategory>(await database.Table<ProductCategory>().ToListAsync());
            return lst;
        }

这是使用 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.

标签: c#xamarinsqlite

解决方案


事实证明这毕竟是 XAML 错误。标记中的元素在 的标记<ListView><DataTemplate></DataTemplate>。在其中添加<ViewCell></ViewCell>和标签解决了这个问题。感谢线程中的 JMK。


推荐阅读