Bug 33010 - A custom element using an IEnumerable crashes when run as a Windows Universal Application
Summary: A custom element using an IEnumerable crashes when run as a Windows Universal...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 1.4.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL: https://bitbucket.org/tvothcalico/win...
Depends on:
Blocks:
 
Reported: 2015-08-12 12:46 UTC by tvoth
Modified: 2016-02-03 22:20 UTC (History)
4 users (show)

Tags: Windows ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description tvoth 2015-08-12 12:46:47 UTC
Using a simple application, I've been able to verify that there is an issue when using an IEnumerable inside a custom element there is a crash when running as a Windows Universal application but not a Android, iOs, or Windows Phone application.  While this code is manually controlling the Views and ViewModels, our actual code uses AutoFac and tries to keep a single instance of the view and viewmodels rather than recreating them every time.

The code can be located at https://bitbucket.org/tvothcalico/winuniversal-xamarin-bug

The custom element in this project (FormGrid) is a simplified version of the code to show the issue.  It has 2 bindable properties; an IEnumerable ItemsSource that allows the end user to pass in a list of items and an ItemTemplate that allows the end user to specify a how they would like the design to look (much like ListView).  The FormGrid will layout a 3x3 grid that will fill the available space and pace items in the blocks.

The simplest steps to reproduce the crashes are as follows...
1) Start the app.
2) Load the sub view with the "Sub-Page" button.
3) Use the back icon to navigate to the main page.
4) Load the sub view with the "Sub-Page" button again.
5) Switch the data in the IEnumerable via the "Switch Data" button.

The problem appears to come in when loading the SubView page for the second time. In DemoApp.xaml.cs there is a method GoToSubPage() where the different methods of loading the subpage causes different issues in Windows Universal (any method works in Android, iOs, and WinPhone).

When the code is in the current state (acting as if a single instance is used for both the View and the ViewModel) the following crash occurs when switching the data on the sub page.
Code:
        private SubPage _subPage = null;
        public void GoToSubPage()
        {
            if (_subPage == null)
            {
                _subPage = new SubPage();
                _subPage.BindingContext = new MainViewModel(this);
            }
            NavigateTo(_subPage);
        }
Crash:
System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=Value does not fall within the expected range.
  Source=Windows
  StackTrace:
       at Windows.UI.Xaml.Controls.Border.put_Child(UIElement value)
       at Xamarin.Forms.Platform.WinRT.FrameRenderer.PackChild()
       at Xamarin.Forms.Platform.WinRT.FrameRenderer.OnElementChanged(ElementChangedEventArgs`1 e)
       at Xamarin.Forms.Platform.WinRT.VisualElementRenderer`2.SetElement(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.RendererFactory.CreateRenderer(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e)
       at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
       at Xamarin.Forms.Element.OnChildAdded(Element child)
       at Xamarin.Forms.Layout.OnInternalAdded(View view)
       at Xamarin.Forms.Layout.InternalChildrenOnCollectionChanged(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 Xamarin.Forms.Grid.GridElementCollection.Add(View view, Int32 left, Int32 right, Int32 top, Int32 bottom)
       at Xamarin.Forms.Grid.GridElementCollection.Add(View view, Int32 left, Int32 top)
       at Calicosol.FillGrid.UpdateGrid(FillGrid grid, IEnumerable source, DataTemplate template)
       at Calicosol.FillGrid.OnItemsSourceChanged(BindableObject bindable, IEnumerable oldValue, IEnumerable newValue)
       at Xamarin.Forms.BindableProperty.<>c__DisplayClass5`2.<Create>b__1(BindableObject bindable, Object oldValue, Object newValue)
       at Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
       at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
       at Xamarin.Forms.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)
       at Xamarin.Forms.BindingExpression.Apply(Boolean fromTarget)
       at Xamarin.Forms.BindingExpression.BindingExpressionPart.<PropertyChanged>b__c()
       at Xamarin.Forms.Platform.WinRT.WindowsBasePlatformServices.<>c__DisplayClass1.<BeginInvokeOnMainThread>b__0()
  InnerException: 

When the code is in the following state (acting as if a single instance is used for the View but a new ViewModel is created each time) the following crash occurs when loading the sub page for the second time.
Code: 
        private SubPage _subPage = null;
        public void GoToSubPage()
        {
            if (_subPage == null)
            {
                _subPage = new SubPage();
            }
            _subPage.BindingContext = new MainViewModel(this);
            NavigateTo(_subPage);
        }
Crash:
System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=Value does not fall within the expected range.
  Source=Windows
  StackTrace:
       at Windows.UI.Xaml.Controls.Border.put_Child(UIElement value)
       at Xamarin.Forms.Platform.WinRT.FrameRenderer.PackChild()
       at Xamarin.Forms.Platform.WinRT.FrameRenderer.OnElementChanged(ElementChangedEventArgs`1 e)
       at Xamarin.Forms.Platform.WinRT.VisualElementRenderer`2.SetElement(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.RendererFactory.CreateRenderer(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.Load()
       at Xamarin.Forms.Platform.WinRT.VisualElementRenderer`2.SetElement(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.RendererFactory.CreateRenderer(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.Load()
       at Xamarin.Forms.Platform.WinRT.VisualElementRenderer`2.SetElement(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.RendererFactory.CreateRenderer(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e)
       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.Load()
       at Xamarin.Forms.Platform.WinRT.PageRenderer.SetElement(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.RendererFactory.CreateRenderer(VisualElement element)
       at Xamarin.Forms.Platform.WinRT.VisualElementExtensions.GetOrCreateRenderer(VisualElement self)
       at Xamarin.Forms.Platform.WinRT.NavigationPageRenderer.SetPage(Page page, Boolean isAnimated, Boolean isPopping)
       at Xamarin.Forms.Platform.WinRT.NavigationPageRenderer.OnPushRequested(Object sender, NavigationRequestedEventArgs e)
       at Xamarin.Forms.NavigationPage.<PushAsyncInner>d__16.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Xamarin.Forms.NavigationPage.<PushAsync>d__11.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at Calicosol.DemoApp.<NavigateTo>d__1.MoveNext()
  InnerException: 

When the code is in the following state (acting as if a new instance is used for the View and ViewModel is created each time) there is no crash.  While we are aware of this workaround it is not desired in our larger application.
Code:
        private SubPage _subPage = null;
        public void GoToSubPage()
        {
            _subPage = new SubPage();
            _subPage.BindingContext = new MainViewModel(this);
            NavigateTo(_subPage);
        }

Crash: NONE
Comment 1 tvoth 2015-08-24 13:55:33 UTC
This issue also exists with Xamarin.Forms 1.5.0.6401-pre2.  See the branch "feature-update-forms-version" for the code that performs this update.
Comment 2 Jon Goldberger [MSFT] 2015-09-09 19:51:22 UTC
I was able to confirm this issue using the steps in the bug description.

However in the test project as is, it seemed that the WinStore Universal app project was trying to reference Forms 1.4.4, but all of the other projects were referencing Forms 1.4.3. Updating all Forms packages to 1.4.4 for all projects allowed the WinStore app project to build and deploy. Once deployed, following the steps ion the bug description reproduced the described issue. 

Exception received:

>System.ArgumentException was unhandled by user code
>  HResult=-2147024809
>  Message=Value does not fall within the expected range.
>  Source=Windows
>  StackTrace:
>       at Windows.UI.Xaml.Controls.Border.put_Child(UIElement value)
>       at Xamarin.Forms.Platform.WinRT.FrameRenderer.PackChild()
>       at Xamarin.Forms.Platform.WinRT.FrameRenderer.OnElementChanged(ElementChangedEventArgs`1 e)
>       at Xamarin.Forms.Platform.WinRT.VisualElementRenderer`2.SetElement(VisualElement element)
>       at Xamarin.Forms.Platform.WinRT.RendererFactory.CreateRenderer(VisualElement element)
>       at Xamarin.Forms.Platform.WinRT.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e)
>       at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
>       at Xamarin.Forms.Element.OnChildAdded(Element child)
>       at Xamarin.Forms.Layout.OnInternalAdded(View view)
>       at Xamarin.Forms.Layout.InternalChildrenOnCollectionChanged(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 Xamarin.Forms.Grid.GridElementCollection.Add(View view, Int32 left, Int32 right, Int32 top, Int32 bottom)
>       at Xamarin.Forms.Grid.GridElementCollection.Add(View view, Int32 left, Int32 top)
>       at Calicosol.FillGrid.UpdateGrid(FillGrid grid, IEnumerable source, DataTemplate template)
>       at Calicosol.FillGrid.OnItemsSourceChanged(BindableObject bindable, IEnumerable oldValue, IEnumerable newValue)
>       at Xamarin.Forms.BindableProperty.<>c__DisplayClass5`2.<Create>b__1(BindableObject bindable, Object oldValue, Object newValue)
>       at Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
>       at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
>       at Xamarin.Forms.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)
>       at Xamarin.Forms.BindingExpression.Apply(Boolean fromTarget)
>       at Xamarin.Forms.BindingExpression.BindingExpressionPart.<PropertyChanged>b__c()
>       at Xamarin.Forms.Platform.WinRT.WindowsBasePlatformServices.<>c__DisplayClass1.<BeginInvokeOnMainThread>b__0()
>  InnerException: 

(Ended here without any info for inner exception)


## Environment:

Microsoft Visual Studio Professional 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00079

Installed Version: Professional

Visual Basic 2015   00322-40000-00000-AA850
Microsoft Visual Basic 2015

Visual C# 2015   00322-40000-00000-AA850
Microsoft Visual C# 2015

Visual C++ 2015   00322-40000-00000-AA850
Microsoft Visual C++ 2015

Visual F# 2015 RC   00322-40000-00000-AA850
Microsoft Visual F# 2015 RC

Windows Phone SDK 8.0 - ENU   00322-40000-00000-AA850
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   14.0.20626.0
ASP.NET and Web Tools

ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

GenerateUnitTest   1.0
Generates unit test code for methods in classes under test.

GitHub.VisualStudio   1.0
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.0.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.50616.0
Microsoft SQL Server Data Tools

Visual Studio Tools for Universal Windows Apps   14.0.23121.00 D14OOB
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.0.0.1111 (a1dc0d5)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.99.653 (cfa43bb)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   8.13.0.122 (2c6a530)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 3 Jon Goldberger [MSFT] 2015-09-09 20:08:01 UTC
Additinoal note: I tested using Forms 1.4.4 and the "master" branch of the linked repo, I did not test the updated version that uses Forms version 1.5.0.6401-pre2 as noted in comment 1
Comment 4 Igor 2016-01-29 05:44:20 UTC
Any updates about this issue?
Comment 5 E.Z. Hart [MSFT] 2016-02-03 22:20:04 UTC
I was able to reproduce this in XF 1.4.4; updated to XF 2.0.1.6505 and the crash no longer occurs.