Mario Stoilov
Mario Stoilov

Reputation: 3447

Incremental Loading and snapped visual state

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.

Upvotes: 1

Views: 323

Answers (1)

Mario Stoilov
Mario Stoilov

Reputation: 3447

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).

Upvotes: 1

Related Questions