public void Refresh(IBehaviorContext context, RefreshOptions options)
        {
            var collection = GetValue(context);

            Repopulate(context, collection, RefreshReason.Create(options.ExecuteRefreshDependencies));

            this.RefreshNext(context, options);
        }
        public void Refresh(IBehaviorContext context, RefreshOptions options)
        {
            // Call next behavior first, because a source accessor behavior may handle
            // it.
            this.RefreshNext(context, options);
            _collectionSourceCache.Clear(context);

            // ToArray so that (1) source is only enumerated once and (2) acess by index
            // (required by the equality check) is guaranteed to be fast.
            TItemSource[]           newSourceItems = GetSourceItems(context).ToArray();
            IVMCollection <TItemVM> vmCollection   = GetValue(context);

            IEnumerable <TItemVM> itemsToRefresh = Enumerable.Empty <TItemVM>();

            if (AreCollectionContentsEqual(vmCollection, newSourceItems))
            {
                itemsToRefresh = vmCollection;
            }
            else
            {
                Dictionary <TItemSource, TItemVM> previousItemsBySource = vmCollection.ToDictionary(
                    x => x.Source,
                    _reusabilitySourceComparer
                    );

                List <TItemVM> newItems    = new List <TItemVM>();
                List <TItemVM> reusedItems = new List <TItemVM>();

                foreach (TItemSource s in newSourceItems)
                {
                    TItemVM item;
                    bool    isReusedItem = previousItemsBySource.TryGetValue(s, out item);

                    if (isReusedItem)
                    {
                        reusedItems.Add(item);
                    }
                    else
                    {
                        item = CreateAndInitializeItem(context, s);
                    }

                    newItems.Add(item);
                }

                vmCollection.ReplaceItems(newItems, RefreshReason.Create(options.ExecuteRefreshDependencies));
                itemsToRefresh = reusedItems;
            }

            if (options.Scope.HasFlag(RefreshScope.Content))
            {
                itemsToRefresh
                .ForEach(x => x.Kernel.RefreshWithoutValidation(options.ExecuteRefreshDependencies));
            }
        }
Example #3
0
        public void Refresh(IBehaviorContext context, RefreshOptions options)
        {
            RequireInitialized();

            var previousValue = GetValue(context);

            RefreshCache(context);
            var newValue = GetValue(context);

            if (!Object.Equals(newValue, previousValue))
            {
                var args = ChangeArgs.ViewModelPropertyChanged(
                    _property,
                    ValueStage.ValidatedValue,
                    previousValue,
                    newValue,
                    RefreshReason.Create(options.ExecuteRefreshDependencies)
                    );

                context.NotifyChange(args);
            }

            this.RefreshNext(context, options);
        }