0

데이터베이스에서 목록보기로 10k 항목을 표시하려고합니다. IList 인터페이스를 사용하여 시각화를 시도했습니다. 아이디어는 listview에서 요청한대로 하나씩 항목을 가져 오는 것이 었습니다. (보이는 항목 만). 그러나 VirtualComboList 개체를 만들고 ListView에 할당하면 예외가 발생합니다.WinRT ListView 가상화

Object reference not set to an instance of an object. 

비슷한 코드가 WP8 (Silverlight)에서 정상적으로 작동합니다. 이제 내가 누락 된 부분을 누구에게도 말해 줄 수 있습니까?

public void initializeList() 
{ 
    int ItemsCount = getItemsCountFromDatabase(); 
    VirtualComboList list = new VirtualComboList(ItemsCount); 
    listBox1.ItemsSource = list; //Exception at this line 
} 

내 VirtualComboList 클래스 구현은 IList 인터페이스

class VirtualComboList : IList<string> 
     { 
      int ItemCount; 

      public VirtualComboList(int count) 
      { 
       ItemCount = count; 
      } 

      public int IndexOf(string item) 
      { 
       return -1; 
      } 

      public void Insert(int index, string item) 
      { 

      } 

      public void RemoveAt(int index) 
      { 

      } 

      public string this[int index] 
      { 
       get 
       { 
        return getStringFromDatabaseForIndex(index); 
       } 
       set 
       { 

       } 
      } 

      public void Add(string item) 
      { 

      } 

      public void Clear() 
      { 

      } 

      public bool Contains(string item) 
      { 
       return false; 
      } 

      public void CopyTo(string[] array, int arrayIndex) 
      { 

      } 

      public int Count 
      { 
       get { return ItemCount; } 
      } 

      public bool IsReadOnly 
      { 
       get { return true ; } 
      } 

      public bool Remove(string item) 
      { 
       return true; 
      } 

      public IEnumerator<string> GetEnumerator() 
      { 
       return null; 
      } 

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

     } 

답변

0

당신은 GetEnumerator 방법 null를 반환해야합니다. 그러지 마. ItemsControl (이 경우 ListView)은 Enumeratornull을 사용하여 목록을 반복하려고 시도하며 예외가 발생합니다.

GetEnumerator의 올바른 구현을 조사해야합니다. 또는 사용자 정의 클래스의 필드/속성으로 List<string>을 가질 수 있으며 기본 컬렉션의 메소드를 노출하기 만하면 직접 구현하지 않아도됩니다.

+1

예, GetEnumerator는 null을 반환하지 않아야합니다. 내 잘못입니다. –

관련 문제