ISupportIncrementalLoading收集 - 通知UI时LoadingMoreItems正在进行中 [英] ISupportIncrementalLoading Collection - notify UI when LoadingMoreItems is in progress

查看:342
本文介绍了ISupportIncrementalLoading收集 - 通知UI时LoadingMoreItems正在进行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序创建一个 IncrementalLoadingCollection 类,它实现 ISupportIncrementalLoading 和<$ C $继承C>的ObservableCollection< T> 。

I've have created a IncrementalLoadingCollection class in my app which implements ISupportIncrementalLoading and inherits from ObservableCollection< T >.

它工作正常和项目加载,但我想显示在消息应用程序的状态栏有正在进行的一些工作。

It works fine and the items are loaded but I would like to show a message on the app's Status Bar that there is some work in progress.

什么是实现这一目标的一个好方法吗?当列表滚动

What's a good way of achieving this?

由于 LoadMoreItemsAsync 在内部调用,我无法访问的那部分拿出代码该更新状态栏。现在,我在 LoadMoreItemsAsync ,我觉得这是一个可怕的方式这样做,但我无法找到一个更好的,到目前为止...

Since LoadMoreItemsAsync is called internally when the list is scrolled, I cannot access that part to come up with the code which updates the Status Bar. Right now, I am doing this in LoadMoreItemsAsync which I find it a terrible approach, but I couldn't find a better one so far...

任何建议高度赞赏

推荐答案

那么你可以,例如:从<$继承C $ C>的ObservableCollection 和实施 ISupportIncrementalLoading 是这样的:

Well you can for example: inherit from ObservableCollection and implement ISupportIncrementalLoading like this:

class IncrementalLoadingObservableCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
{
    private readonly Func<CancellationToken, Task<IEnumerable<T>>> _provideMoreItems;

    public IncrementalLoadingObservableCollection(Func<CancellationToken, Task<IEnumerable<T>> provideMoreItems)
    {
        _provideMoreItems = provideMoreItems;
    }

    public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
    {
        return AsyncInfo.Run(async cancelToken =>
        {
            await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                OnLoadMoreItemsStarted();
            });

            var providedItems = await _provideMoreItems(cancelToken);

            await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                foreach(var item in providedItems)
                    Add(item);

                OnLoadMoreItemsCompleted();
            });

            return new LoadMoreItemsResult {Count = (uint) providedItems.Count()};;
        });
    }

    public bool HasMoreItems
    {
        get { return true; }
    }

    public event Action LoadMoreItemsStarted;
    public event Action LoadMoreItemsCompleted;

    protected virtual void OnLoadMoreItemsStarted()
    {
        var handler = LoadMoreItemsStarted;
        if (handler != null) handler();
    }

    protected virtual void OnLoadMoreItemsCompleted()
    {
        var handler = LoadMoreItemsCompleted;
        if (handler != null) handler();
    }
}



如何使用它呢?在您的视图模型:

How to use it? In your ViewModel:

class MyFancyItemsViewModel
{
     public MyFancyItemsViewModel()
     {  
         var incrementalObservablCollcetion = new IncrementalLoading...(GetItemsFromInternetOrSmth);
         incrementalObservablCollcetion.LoadMoreItemsStarted += OnItemsLoadingStarted;
         incrementalObservablCollcetion.LoadMoreItemsCompleted += OnItemsLoadingCompleted;

          ItemsBindedInXaml = incrementalObservablCollcetion;
     }

     private Task<IEnumerable<Items>> GetItemsFromInternetOrSmth(CancellationToken cancelToken)
     {
      ... do some work returns enumerable of Items
     }

     private void OnItemsLoadingStarted()
     { .. do smth .. }

     private void OnItemsLoadingCompleted()
     { ... do smth .. } 

     public ObservableCollection<Items> ItemsBindedInXaml { get; private set; }
}

您可能会问,为什么我用调度。 RunAsync IncrementalLoadingObservableCollection - 原因是 LoadMoreItemsAsync 可能会在另一个线程上运行(唐'知道了),所以你必须派遣所有的工作到 UI线程(这是无法调用从其他线程比 UI相关的方法UI螺纹不使用调度)的

You might ask why I have used Dispatcher.RunAsync in IncrementalLoadingObservableCollection - the reason is that LoadMoreItemsAsync might run on another thread (don't know that) so you have to dispatch all the work to the UI Thread (it's not possible to call UI-related methods from thread other than UI thread without use of Dispatcher).

如果您觉得视图模型未appropiate地方UI相关的操作看一看一些通讯机制(如 MVVM光使者,注册在代码 - 消息后面和 LoadMoreItemsStarted 处理)

If you feel that ViewModel is not appropiate place for UI-related operations take a look at some messaging mechanisms (like MVVM Light Messenger, register message in code-behind and send this message in LoadMoreItemsStarted handler)

这篇关于ISupportIncrementalLoading收集 - 通知UI时LoadingMoreItems正在进行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆