Domanda

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).

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top