【问题标题】:"Specified cast is invalid" when retrieving object from sqlite3从 sqlite3 检索对象时“指定的转换无效”
【发布时间】:2019-03-02 17:32:45
【问题描述】:

对不起,如果以前有人问过这个问题,现在搜索了好几个小时都没有结果。

我正在使用 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.

【问题讨论】:

  • 什么是完整的 InvalidCastException 消息?
  • 我建议您首先将该复合语句分解为离散的步骤,并确定哪个特定的步骤导致异常
  • 我添加了控制台的输出。还有其他方法可以获取更多数据吗?
  • Jason,您的确切意思是什么?表已创建。 SaveCategoryAsync(ProductCategory category) 按预期工作,添加产品并且 sqlite 自动增加 id 属性。我的理解是我应该从 GetProductCategoriesAsync() 收到一个列表,因为这正是我从同一张表中收到的。我无法在方法中捕获单个断点,因为语句行只是中断了调试会话,出现了 invalidCastException 错误并且没有详细信息。

标签: c# xamarin sqlite


【解决方案1】:

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

【讨论】:

  • 请将您的解决方案标记为已回答。帮助其他人搜索需要帮助的问题,这样他们就不会在已回答的问题上浪费时间。
猜你喜欢
  • 2011-03-02
  • 2010-11-02
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多