diff --git a/PanCardView/CardsView.cs b/PanCardView/CardsView.cs index 167de98..e2c7c0f 100644 --- a/PanCardView/CardsView.cs +++ b/PanCardView/CardsView.cs @@ -758,7 +758,7 @@ protected virtual void SetAllViews(bool shouldCleanUnprocessingChildren) } else { - Processor?.Clean(this, new ProcessorItem { Views = oldAllViews.Where(view => !CheckIsProcessingView(view)) }); + Processor.Clean(this, new ProcessorItem { Views = oldAllViews.Where(view => !CheckIsProcessingView(view)) }); } } } @@ -929,7 +929,7 @@ protected virtual async Task TryAutoNavigate() await Task.Delay(5); _currentBackAnimationDirection = realDirection; - await (Processor?.Navigate(this, GetAnimationProcessorItems()) ?? Task.FromResult(true)); + await InvokeOnMainThreadIfNeededAsync(() => Processor.Navigate(this, GetAnimationProcessorItems())); PerformUWPFrontViewProcessorHandlePanChanged(0, realDirection); EndAutoNavigation(views, animationId, animationDirection); return true; @@ -978,7 +978,7 @@ protected override void OnSizeAllocated(double width, double height) prevAnimationDirection = AnimationDirection.Next; nextAnimationDirection = AnimationDirection.Prev; } - Processor?.Init(this, + Processor.Init(this, new ProcessorItem { IsFront = true, Views = Enumerable.Repeat(CurrentView, 1) }, new ProcessorItem { Views = PrevViews, Direction = prevAnimationDirection }, new ProcessorItem { Views = NextViews, Direction = nextAnimationDirection }); @@ -1007,7 +1007,7 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName return; } AdjustSlideShow(true); - Processor?.Clean(this, new ProcessorItem { Views = GetNextPrevCurrentViews() }); + Processor.Clean(this, new ProcessorItem { Views = GetNextPrevCurrentViews() }); return; } } @@ -1256,7 +1256,7 @@ private void OnTouchChanged(double diff, double oppositeDirectionDiff, bool isTo try { BatchBegin(); - Processor?.Change(this, diff, GetAnimationProcessorItems()); + Processor.Change(this, diff, GetAnimationProcessorItems()); } finally { @@ -1321,12 +1321,12 @@ private async void OnTouchEnded() SelectedIndex = index; - endingTask = Processor?.Proceed(this, GetAnimationProcessorItems()) ?? Task.FromResult(true); + endingTask = Processor.Proceed(this, GetAnimationProcessorItems()); } else { endingTask = interactionItem.IsInvolved - ? Processor?.Reset(this, GetAnimationProcessorItems()) ?? Task.FromResult(true) + ? Processor.Reset(this, GetAnimationProcessorItems()) : Task.FromResult(true); } @@ -1604,7 +1604,7 @@ private IEnumerable InitViews(bool isFront, AnimationDirection animationDi return Enumerable.Empty(); } - Processor?.Init(this, new ProcessorItem { IsFront = isFront, Views = views, Direction = animationDirection }); + Processor.Init(this, new ProcessorItem { IsFront = isFront, Views = views, Direction = animationDirection }); SetupLayout(views); @@ -1681,7 +1681,7 @@ private View CreateRetrieveView(object context, DataTemplate template, IEnumerab var duplicatedViews = notUsingViews .Except(Enumerable.Repeat(view, 1)) .Where(v => Equals(GetItem(v), GetItem(view))); - Processor?.Clean(this, new ProcessorItem { Views = duplicatedViews }); + Processor.Clean(this, new ProcessorItem { Views = duplicatedViews }); } return view; @@ -1780,7 +1780,7 @@ private void CleanView(View view) view.BindingContext = null; } view.Behaviors.Remove(_contextAssignedBehavior); - Processor?.Clean(this, new ProcessorItem { Views = Enumerable.Repeat(view, 1) }); + Processor.Clean(this, new ProcessorItem { Views = Enumerable.Repeat(view, 1) }); } private void SetItemsSource(IEnumerable oldCollection) @@ -1968,6 +1968,11 @@ private void InvokeOnMainThreadIfNeeded(Action action) Device.BeginInvokeOnMainThread(action); } + private Task InvokeOnMainThreadIfNeededAsync(Func action) + => !Device.IsInvokeRequired + ? action.Invoke() + : Device.InvokeOnMainThreadAsync(action); + private void ExecutePreventException(Action action) { try @@ -1999,7 +2004,7 @@ private void PerformUWPFrontViewProcessorHandlePanChanged(double value, Animatio { return; } - Processor?.Change(this, value, + Processor.Change(this, value, new ProcessorItem { IsFront = true,