xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] IndexOutOfRangeException on TabbedPage with UWP (Forms 4.0) #6323

Closed SebastianKruseWago closed 5 years ago

SebastianKruseWago commented 5 years ago

Description

After updating to Xamarin.Forms 4.0 my UWP project is no longer able to show TabbedPages (IndexOutOfRangeException).

Steps to Reproduce

  1. Setup Xamarin.Forms 4.0 with UWP
  2. Set SelectedTabColor, UnselectedTabColor, ... of the TabbedPage
  3. Navigate to the TabbedPage

Expected Behavior

No exception + working

Actual Behavior

Crashes with this exception:

Index was outside the bounds of the array.
   at Xamarin.Forms.Platform.UWP.TabbedPageRenderer.UpdateSelectedTabColors()
   at Xamarin.Forms.Platform.UWP.TabbedPageRenderer.OnSelectionChanged(Object sender, SelectionChangedEventArgs e)
   at Windows.UI.Xaml.Controls.ItemsControl.OnItemsChanged(Object e)
   at System.Runtime.InteropServices.WindowsRuntime.IVector`1.InsertAt(UInt32 index, T value)
   at System.Runtime.InteropServices.WindowsRuntime.VectorToListAdapter.InsertAtHelper[T](IVector`1 _this, UInt32 index, T item)
   at System.Runtime.InteropServices.WindowsRuntime.VectorToListAdapter.Insert[T](Int32 index, T item)
   at Xamarin.Forms.Internals.NotifyCollectionChangedEventArgsExtensions.<>c__DisplayClass0_0`1.<Apply>b__0(Object o, Int32 i, Boolean b)
   at Xamarin.Forms.Internals.NotifyCollectionChangedEventArgsExtensions.Apply(NotifyCollectionChangedEventArgs self, Action`3 insert, Action`2 removeAt, Action reset)
   at Xamarin.Forms.Internals.NotifyCollectionChangedEventArgsExtensions.Apply[TFrom](NotifyCollectionChangedEventArgs self, IList`1 from, IList`1 to)
   at Xamarin.Forms.Platform.UWP.TabbedPageRenderer.OnPagesChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at Xamarin.Forms.ObservableWrapper`2.ListOnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at Xamarin.Forms.ObservableWrapper`2.Add(TRestrict item)
   at MvvmCross.Forms.Presenters.MvxFormsPagePresenter.ShowTabbedPage(Type view, MvxTabbedPagePresentationAttribute attribute, MvxViewModelRequest request)
   at MvvmCross.Base.MvxMainThreadAsyncDispatcher.<>c__DisplayClass1_0.<<ExecuteOnMainThreadAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_0(Object state)
   at System.Threading.WinRTSynchronizationContextBase.Invoker.InvokeCore()

Basic Information

MitchBomcanhao commented 5 years ago

I've had the same issue. This was introduced by https://github.com/xamarin/Xamarin.Forms/pull/4899 as far as I can tell.

Whenever we have a blank tabbed page and add items to its items source, the app will crash with said index out of range exception in the tabbed page renderer.

Workaround: this has forced us to create the items source with a dummy blank tab and then remove it when the proper tabs are created later on.

SebastianKruseWago commented 5 years ago

@MitchBomcanhao Another workaroud is to create the TabbedPageRenderer yourself (basicly a copy of the original one) and check the page count. The problem is the following line: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs#L563 (not tested yet but this is the cause of the issue based on the StackTrace).

samhouts commented 5 years ago

Issue6323.zip

samhouts commented 5 years ago

closed by #6385