0

I have a large array of Integer items that I want to display in ListView with formatting. Normally on Android I would extend BaseAdapter (this is an example of dynamic View/Control creation):

public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView!=null){
        //inflate convertView if it doesn't exist 
    }
    MyView v = (MyView)convertView;
    v.setId(position);
    v.setNumber(myIntegerArray[position]);
    return v;
}

I would like to know how to do this in C# for my Windows Store app. I could not find any meaningful information on this topic.

I do not want to create List (or other ItemsSource object) everytime, beacuse it will take too much memory and it will be slow (I have to refresh array values).

Chris Miemiec
  • 755
  • 1
  • 9
  • 19

1 Answers1

1

XAML-based UI technologies have a capability called UI Virtualization which reduces memory consumption and processing time when dealing with large collections of data presented in items-based UIs (ItemsControls).

In order to allow this, while keeping the capability to update the UI whenever items are added/removed/changed in the underlying data collection, you need to DataBind your UI to an ObservableCollection<T>:

<ListBox ItemsSource="{Binding}"/>

code behind:

var numbers = Enumerable.Range(0,100);
DataContext = new ObservableCollection<int>(numbers);

Keep in mind that the declarative, DataBinding-based approach in XAML based UI technologies is really different from the procedural approach in other frameworks. In XAML, you never need to create UI elements in procedural code, you define them in XAML and then DataBind the UI to a relevant collection of Data Items, and the UI framework takes care of the rest.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Alright, I created ObservableCollection() where MyCustomType implements required fields. Now I would like the ListView to automatically update when change occurs in ObservableCollection. How do I do that (in Android it is called notifyDataSetChanged() ). – Chris Miemiec Mar 10 '14 at 21:57
  • @rs411 your `MyCustomType` needs to [Implement `INotifyPropertyChanged`](http://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx). – Federico Berasategui Mar 10 '14 at 21:58
  • @rs411, BTW, you should stop thinking about Android, unfortunately the entire Android framework is based on a limited, bloated, inefficient, inexpressive, overly verbose language called "java" and it suffers a lot from the limitation of such language. XAML-based technologies have completely different approaches. – Federico Berasategui Mar 10 '14 at 22:00
  • Yep, I've noticed that C# is way better while instead of writing `new Runnable(){@Override public void run(){/*my code*/}}` I just had to write `delegate {/*my code*/}`. And I don't think about abandoning Android (I really like this platform), but as I now have Windows 8.1 tablet I decided to create some software for it. :D – Chris Miemiec Mar 10 '14 at 22:08
  • 1
    @rs411 `delegate {/*my code*/}` can be replaced by `() => /*mycode*/`, or `x => x.Method()` where x is an instance of a class that contains a method called `Method()` depending on the parameters of the delegate, which the C# compiler infers instead of forcing you to write a bunch of boilerplate. – Federico Berasategui Mar 10 '14 at 22:15
  • I knew that before, lambda expressions are great. :) I've even seen them while watching some Java 8 previews, but unfortunately Dalvik compiler is still Java 6 (not 7!). I hope that when ART fully rolls out, Java 8 support will come with it... – Chris Miemiec Mar 10 '14 at 22:25