简体   繁体   English

Windows 8 Store Apps 中 c# CollectionBase 的替代品是什么?

[英]what is the replacement of c# CollectionBase in windows 8 Store Apps?

I'm Navigating a Class from windows Forms app to windows store app.我正在将一个类从 Windows 窗体应用程序导航到 Windows 应用商店应用程序。 The Class i've got from internet is shown below,我从互联网上得到的课程如下所示,

    public class ElementList : CollectionBase
{
    /// <summary>
    /// A Collection of Element Nodes
    /// </summary>      
    public ElementList() 
    {           
    }

    public void Add(Node e) 
    {
        // can't add a empty node, so return immediately
        // Some people tried dthis which caused an error
        if (e == null)
            return;

        this.List.Add(e);
    }

    // Method implementation from the CollectionBase class
    public void Remove(int index)
    {
        if (index > Count - 1 || index < 0) 
        {
            // Handle the error that occurs if the valid page index is       
            // not supplied.    
            // This exception will be written to the calling function             
            throw new Exception("Index out of bounds");            
        }        
        List.RemoveAt(index);           
    }

    public void Remove(Element e)
    {           
        List.Remove(e);         
    }

    public Element Item(int index) 
    {
        return (Element) this.List[index];
    }


}

In the above class the CollectionBase is not accepted in the store app.在上面的类中,商店应用程序不接受 CollectionBase。 Please tell me a way to navigate this to windows 8 store app.请告诉我一种导航到 Windows 8 商店应用程序的方法。 . . . .

Thanks in Advance!提前致谢!

you don't need to use你不需要使用

    IList 

instead you can use相反,你可以使用

    List<Object>. . .

Just give it a try.试试看吧。 . . . .

It worked for me May be it works for you too..它对我有用可能对你也有用..

you should use System.Collections.ObjectModel or System.Collections.Generic in WinRT你应该在 WinRT 中使用System.Collections.ObjectModelSystem.Collections.Generic

CollectionBase is obsolete and you should avoid using it. CollectionBase已过时,您应该避免使用它。

I think i actually figured it, the CollectionBase inherited from IList, so i rewrite the code as follows,我想我真的想通了,CollectionBase继承自IList,所以我重写了如下代码,

    public class ElementList
{
    public IList List { get; }
    public int Count { get; }


    public ElementList()
    {

    }

    public void Add(Node e)
    {
        if (e == null)
        {
            return;
        }

        this.List.Add(e);
    }

    public void Remove(int index)
    {
        if (index > Count - 1 || index < 0)
        {
            throw new Exception("Index out of bounds");
        }
        List.RemoveAt(index);           
    }

    public void Remove(Element e)
    {
        List.Remove(e);
    }

    public Element Item(int index)
    {
        return (Element)this.List[index];
    }

}

As an alternative you can always write your own CollectionBase that does the same thing.作为替代方案,您始终可以编写自己的CollectionBase来做同样的事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM