ISupportIncrementalLoading 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为支持增量加载的集合视图指定调用协定。
public interface class ISupportIncrementalLoading
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(3640243590, 42570, 24568, 134, 142, 32, 78, 20, 79, 44, 244)]
struct ISupportIncrementalLoading
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.Guid(3640243590, 42570, 24568, 134, 142, 32, 78, 20, 79, 44, 244)]
public interface ISupportIncrementalLoading
Public Interface ISupportIncrementalLoading
- 属性
示例
下面的代码示例演示如何实现此接口。 有关完整的代码列表,请参阅 XAML 数据绑定示例。
#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
属性
HasMoreItems |
获取支持增量加载实现的 sentinel 值。 |
方法
LoadMoreItemsAsync(UInt32) |
从视图初始化增量加载。 |