Incremental Loading and snapped visual state

拥有回忆 提交于 2019-12-11 10:58:37

问题


I have a listview(for snapped state) and a gridview(for normal state) in my app page (which is an items page template) and they are both bound to a collection that implements ISupportIncrementalLoading interface. Here are the implemented methods:

public HasMoreItemsDelegate MoreItemsExist { get; set; }
        public LoadMoreItemsDelegate<T> ItemsLoadAsync { get; set; }

        public bool HasMoreItems
        {
            get 
            {
                return this.MoreItemsExist();
            }
        }

        private bool isLoading;

        public bool IsLoading
        {
            get
            {
                return this.isLoading;
            }
            private set
            {
                this.isLoading = value;
                this.OnPropertyChanged("IsLoading");
            }
        }

        public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
        {
            if (this.IsLoading)
            {
               throw new InvalidOperationException("Only one operation in flight at a time");
            }
            this.IsLoading = true;
            return AsyncInfo.Run((c) => LoadMoreItemsAsync(c, count));
        }

        async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
        {
            try
            {
                IEnumerable<T> itemsToAdd = await ItemsLoadAsync(c, count);
                foreach (var item in itemsToAdd)
                {
                    this.Add(item);
                }
                return new LoadMoreItemsResult { Count = (uint)itemsToAdd.Count() };
            }
            finally
            {
                this.IsLoading = false;
            }
        }

The problem I encounter is: When the app is in snapped mode and I navigate to the page with the listview and gridview, BOTH views try to call LoadMoreItemsAsync and I enter the exception clause, whereas when in normal (fullscreen) mode, only the gridview calls the method, and the listview doesn't.


回答1:


The problem comes from how the template works. The gridview is Visible by default and the listview is not. When we navigate to the page, the VisualStateManager is called AFTER the page is rendered, and therefore when navigated to, the gridview is visible for a short time and calls the LoadMoreItemsAsync method before it gets hidden. After the storyboard is complete, the listview gets shown and also calls the LoadMoreItemsAsync and the problem occurs.

How I solved it: Made both views Collapsed by default and handled when they are shown in the VisualStateManager (it's ALWAYS called after navigating to the page).



来源:https://stackoverflow.com/questions/19066319/incremental-loading-and-snapped-visual-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!