ISupportIncrementalLoading ISupportIncrementalLoading ISupportIncrementalLoading Interface

Specifies a calling contract for collection views that support incremental loading.

Syntax

Declaration

public interface ISupportIncrementalLoadingpublic interface ISupportIncrementalLoadingPublic Interface ISupportIncrementalLoading

Remarks

Examples

The following code example demonstrates how to implement this interface. For the complete code listing, see the XAML data Binding sample.

#pragma region Overridable methods

        virtual Concurrency::task<Windows::Foundation::Collections::IVector<Platform::Object^>^> LoadMoreItemsOverride(Concurrency::cancellation_token c, unsigned int count)
        {
            return Concurrency::task<Windows::Foundation::Collections::IVector<Platform::Object^>^>(
                [=]() -> Windows::Foundation::Collections::IVector<Platform::Object^>^ {
                    auto items = ref new Platform::Collections::Vector<Platform::Object^>();
                    return items;
            });
        }
        virtual bool HasMoreItemsOverride()
        {
            return false;
        }

#pragma endregion 

        IncrementalLoadingBase()
        {
            _storage = ref new Platform::Collections::Vector<Platform::Object^>();
            _storage->VectorChanged += ref new Windows::Foundation::Collections::VectorChangedEventHandler<Platform::Object^>(this, &IncrementalLoadingBase::_storageVectorChanged);
            _busy = false;
            _isVectorChangedObserved = false;
        }

    public:

        virtual Windows::Foundation::IAsyncOperation<Windows::UI::Xaml::Data::LoadMoreItemsResult>^ LoadMoreItemsAsync(unsigned int count)
        {
            if (_busy)
            {
                throw ref new Platform::FailureException("Only one operation in flight at a time");
            }

            _busy = true;

            return Concurrency::create_async([=](Concurrency::cancellation_token c) {
                return LoadMoreItemsAsync(c, count)
                    .then([=](Windows::UI::Xaml::Data::LoadMoreItemsResult result) -> Windows::UI::Xaml::Data::LoadMoreItemsResult {
                        _busy = false;
                        return result;
                });
            });
        }

        property bool HasMoreItems
        {
            virtual bool get() { return HasMoreItemsOverride(); }
        }

#pragma endregion 
#region ISupportIncrementalLoading

public bool HasMoreItems
{
    get { return HasMoreItemsOverride(); }
}

public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
    if (_busy)
    {
        throw new InvalidOperationException("Only one operation in flight at a time");
    }

    _busy = true;

    return AsyncInfo.Run((c) => LoadMoreItemsAsync(c, count));
}

#endregion 

#region INotifyCollectionChanged

public event NotifyCollectionChangedEventHandler CollectionChanged;

#endregion 

#region Private methods

async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
    try
    {
        var items = await LoadMoreItemsOverrideAsync(c, count);
        var baseIndex = _storage.Count;

        _storage.AddRange(items);

        // Now notify of the new items
        NotifyOfInsertedItems(baseIndex, items.Count);

        return new LoadMoreItemsResult { Count = (uint)items.Count };
    }
    finally
    {
        _busy = false;
    }
}

void NotifyOfInsertedItems(int baseIndex, int count)
{
    if (CollectionChanged == null)
    {
        return;
    }

    for (int i = 0; i < count; i++)
    {
        var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _storage[i + baseIndex], i + baseIndex);
        CollectionChanged(this, args);
    }
}

#endregion

#region Overridable methods

protected abstract Task<IList<object>> LoadMoreItemsOverrideAsync(CancellationToken c, uint count);
protected abstract bool HasMoreItemsOverride();

#endregion 

#region State

List<object> _storage = new List<object>();
bool _busy = false;

#endregion 

Properties summary

Gets a sentinel value that supports incremental loading implementations.

Methods summary

Initializes incremental loading from the view.

Properties

  • HasMoreItems
    HasMoreItems
    HasMoreItems
    HasMoreItems

    Gets a sentinel value that supports incremental loading implementations.

    public bool HasMoreItems { get; }public bool HasMoreItems { get; }Public ReadOnly Property HasMoreItems As bool

    Property Value

    • bool
      bool
      bool

      true if additional unloaded items remain in the view; otherwise, false.

Methods

  • LoadMoreItemsAsync(System.UInt32)
    LoadMoreItemsAsync(System.UInt32)
    LoadMoreItemsAsync(System.UInt32)
    LoadMoreItemsAsync(System.UInt32)

    Initializes incremental loading from the view.

    public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(System.UInt32 count)public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(System.UInt32 count)Public Function LoadMoreItemsAsync(count As System.UInt32) As IAsyncOperation( Of LoadMoreItemsResult )

    Parameters

    • count
      System.UInt32
      System.UInt32
      System.UInt32

      The number of items to load.

    Returns

    • The wrapped results of the load operation.

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.WebHostHiddenAttribute
Windows.Foundation.Metadata.GuidAttribute
Windows.Foundation.Metadata.ContractVersionAttribute

Details

Assembly

Windows.UI.Xaml.Data.dll