Xamarin.Forms Guides | Samples

Xamarin.Forms.ListViewCachingStrategy Enumeration

Enumerates caching strategies for a ListView.

Syntax

public enum ListViewCachingStrategy

Remarks

Application developers can specify one of these values when constructing a ListView to determine whether the List View will minimize their memory footprint and speed execution by recycling list cells, or will instead generate a cell for every item in the list. Currently, the default behavior is to retain item data in their generated cells when they are not needed. (Items are not needed, for example, when they are far enough off screen that their display is not imminent.) This behavior corresponds to a value of ListViewCachingStrategy.RetainElement. For performance reasons, it is likely that the default behavior will be changed to ListViewCachingStrategy.RecycleElement in a future release. In the meantime, for memory and performance reasons, app developers should specify ListViewCachingStrategy.RecycleElement when constructing a new List View.

The performance advantage of ListViewCachingStrategy.RecycleElement is so great that application developers have been provided with a XAML syntax shortcut for initializing List Views. Instead of x:TypeArguments syntax that specifies a parameter for the ListView.ListView(ListViewCachingStrategy) constructor, XAML for Xamarin.Forms provides a XAML attribute for a non-existent property that corresponds to the caching strategy argument of the constructor. Application developers can set the CachingStrategy attribute to either of the RecycleElement (preferred) or RetainElement values to choose a caching strategy. For example:

XAML Example

<ListView CachingStrategy="RecycleElement" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <!-- ... -->
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Note: When devolopers specify ListViewCachingStrategy.RecycleElement, OnElementChanged events are not raised when cells are recycled. Instead, the cell is retained and its property values change when the binding context is updated to that of an available cell, OnElementPropertyChanged events are raised. Application developers should remember to listen for the correct events, and should note that their renderers will need to be updated if the default behavior changes to ListViewCachingStrategy.RecycleElement in a future release.

Note: The UWP platform ignores ListViewCachingStrategy.RetainElement, because it always uses caching to improve performance. Therefore, if the developer has opted not to use data bindings, they must use ListView.OnBindingContextChanged to update cell data on UWP, because it always behaves as if ListViewCachingStrategy.RecycleElement was specified.

Members

Member NameDescription
RecycleElementIndicates that unneeded cells will have their binding contexts updated to that of a cell that is needed.
RetainElementIndicates that for every item in the List View's ItemsView<TVisual>.ItemsSource property, a single unique element will be constructed from the DataTemplate.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.ListViewCachingStrategy are listed below.

See Also: Enum

Public Fields

RecycleElementListViewCachingStrategy. Indicates that unneeded cells will have their binding contexts updated to that of a cell that is needed.
RetainElementListViewCachingStrategy. Indicates that for every item in the List View's ItemsView<TVisual>.ItemsSource property, a single unique element will be constructed from the DataTemplate.