简体   繁体   English

使用C#的Windows Phone 7.5 appName.dll中发生类型'System.InvalidCastException'的未处理异常

[英]Windows phone 7.5 using C# An unhandled exception of type 'System.InvalidCastException' occurred in appName.dll

Does anyone know the proper way to cast itemCollection.GetEnumerator()? 有谁知道投射itemCollection.GetEnumerator()的正确方法?

For some reasons, return itemCollection.GetEnumerator(); 由于某些原因,返回itemCollection.GetEnumerator();。 works fine in windows phone 8, but doesn't work in windows phone 7.5 It asked me to explicity cast it, but I'm not sure what is the proper way. 在Windows Phone 8中可以正常工作,但在Windows Phone 7.5中不能正常工作。它要求我显式转换它,但我不确定什么是正确的方法。 Does anyone know? 有人知道吗?

public class ItemCollection : IEnumerable<Object>
{
    private System.Collections.ObjectModel.ObservableCollection<Item> itemCollection = new System.Collections.ObjectModel.ObservableCollection<Item>();

    public IEnumerator<Object> GetEnumerator()
    {
        //return itemCollection.GetEnumerator();
        IEnumerator<Object> test = (IEnumerator<Object>)itemCollection.GetEnumerator();

        return test;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Add(Item item)
    {
        itemCollection.Add(item);
    }
}

我不确定,请尝试一下

IEnumerator<Object> test = new IEnumerator<Object>(itemCollection.GetEnumerator());

另外,试试这个

return itemCollection.Cast<object>()

hmm it not clear to me why you want to do this: wouldn't it be better to just do this: 嗯,我不清楚您为什么要这样做:这样做不是更好:

public class ItemCollection : System.Collections.ObjectModel.ObservableCollection<Item>
{
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UretimPlan.exe 中出现“System.InvalidCastException”类型的未处理异常 - An unhandled exception of type 'System.InvalidCastException' occurred in UretimPlan.exe .DLL中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidCastException' occurred in .DLL but was not handled in user code 类型为“ System.InvalidCastException”的未处理异常 - An unhandled exception of type 'System.InvalidCastException' 发生类型为&#39;System.InvalidCastException&#39;的未处理异常…无法将类型为&#39;System.String&#39;的对象转换为类型 - unhandled exception of type 'System.InvalidCastException' occurred…Unable to cast object of type 'System.String' to type 从Datagrid到另一个Winform发生类型为&#39;System.InvalidCastException&#39;的未处理异常 - An unhandled exception of type 'System.InvalidCastException' occurred From Datagrid to Another Winform 我不断收到mscorlib.dll中发生的&#39;System.InvalidCastException&#39;类型的异常,但未在RetailPrice的用户代码中处理 - I keep getting An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code for RetailPrice WindowsBase.dll中出现'System.InvalidCastException'类型的第一次机会异常 - A first chance exception of type 'System.InvalidCastException' occurred in WindowsBase.dll EntityFramework.Core.dll中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidCastException' occurred in EntityFramework.Core.dll but was not handled in user code 发生未处理的异常C#的system.data.dll中发生了类型&#39;system.invalidoperationexception&#39;的未处理异常 - An unhandled exception occured an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll in c# 类型&#39;System.InvalidCastException&#39;的异常 - An exception of type 'System.InvalidCastException'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM