Bug 27642 - [Windows Phone] Adding a ScrollView control to a ContentView, remove it and re-add it will cause an exception on Windows Phone
Summary: [Windows Phone] Adding a ScrollView control to a ContentView, remove it and r...
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.5
Hardware: PC Mac OS
: Normal critical
Target Milestone: ---
Assignee: Rui Marinho
URL:
Depends on:
Blocks:
 
Reported: 2015-03-04 09:07 UTC by John Miller [MSFT]
Modified: 2015-10-06 14:48 UTC (History)
12 users (show)

Tags: windows crash scrollview ac
Is this bug a regression?: ---
Last known good build:


Attachments
Test Case (167.94 KB, application/zip)
2015-03-04 09:07 UTC, John Miller [MSFT]
Details
Zip Xamarin Log (493.16 KB, application/octet-stream)
2015-03-12 14:02 UTC, asimk
Details
Sample Reproduce Scroll View Bug (6.09 MB, application/x-zip-compressed)
2015-03-30 12:45 UTC, Thomas Mutzl
Details


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:
VERIFIED FIXED

Description John Miller [MSFT] 2015-03-04 09:07:42 UTC
Created attachment 10147 [details]
Test Case

**Overview:**

   The attached sample will throw an exception when run on a Windows Phone. A ScrollView is added to a ContentView and set as the Content of a page.

**Steps to Reproduce:**

   1. Run the attached sample on Windows Phone (The ScrollViewIssue.WinPhone project)
   2. Touch the View B button
   3. Touch the View A button
   4. Touch the View B button again

**Actual Results:**

   An Argument not in range exception occurs. 

**Expected Results:**

   No exceptions

**Build Date & Platform:**

   XF 1.3.5
   Windows Phone

**Additional Information:**

   Works on Android.
Comment 1 asimk 2015-03-12 14:02:03 UTC
Created attachment 10307 [details]
Zip Xamarin Log

I have checked this issue with attached sample app and able to reproduce it. I observed that when I press the "View B" button then press "View A" button again press "View B" button, I observed that 'System.ArgumentException' has been thrown.

Please let me know if I can provide any other info/log.

I have checked this issue with xamarin forms version 1.4.0.

Screencast: http://www.screencast.com/t/JLyLy3WiU0

Environment Info:
Microsoft Visual Studio Professional 2013
Version 12.0.30723.00 Update 3
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Professional

Xamarin   3.9.517.0 (856798e)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   4.20.0.37 (9e05e39f02bafe8fc0b7ab025d99f3b446835ad4)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   8.8.1.0 (61fa2c6e0f2ef51eb98b8810de13b4469f379cc7)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified
Comment 2 Thomas Mutzl 2015-03-30 04:43:37 UTC
I have the same issue with XF.

When I reassign a view that contains a ScrollView, I get an exception on Android:

Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Comment 3 Thomas Mutzl 2015-03-30 12:45:00 UTC
Created attachment 10561 [details]
Sample Reproduce Scroll View Bug

I created an app for Android and WP, that demos the bug in a few lines.
Description in the startscreen of the app.
Comment 4 Brendan Zagaeski (Xamarin Team, assistant) 2015-03-30 22:41:24 UTC
The Android test case from comment 3 does indeed fail on Xamarin.Forms 1.3.1.6296, but it does _not_ fail on more recent versions (tested on Xamarin.Forms 1.4.1.6349).

The Windows Phone test case from comment 3 does still fail as of Xamarin.Forms 1.4.1.6349, and the top of the stack trace is the same as in the original test case from comment 0.


## Notes about the test case from comment 0

- It looks like the StackLayout within the ScrollView becomes "stale" after the first time it's used to set `System.Windows.Controls.ContentControl.Content` (in `ScrollViewRenderer.LoadContent()`).


- The second time `ScrollViewRenderer.LoadContent()` tries to set `System.Windows.Controls.ContentControl.Content` to the StackLayout, it fails.


- It's possible to hack around the problem re-creating the StackLayout and re-adding it to viewB's ScrollView each time the "View B" button is tapped.



## Related bug

Bug 26233 looks like it might be closely related, but that bug does not currently include a test case.



## Top of the exception message + stack trace from the test case in comment 0.

> System.ArgumentException was unhandled by user code
>   HResult=-2147024809
>   Message=Value does not fall within the expected range.
>   Source=System.Windows
>   StackTrace:
>        at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
>        at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
>        at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
>        at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
>        at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
>        at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
>        at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
>        at System.Windows.Controls.ContentControl.set_Content(Object value)
>        at Xamarin.Forms.Platform.WinPhone.ScrollViewRenderer.LoadContent()
>        at Xamarin.Forms.Platform.WinPhone.ScrollViewRenderer.OnElementChanged(ElementChangedEventArgs`1 e)
Comment 5 Rui Marinho 2015-09-24 16:48:43 UTC
Should be fixed in 1.5.1-pre1
Comment 6 Parmendra Kumar 2015-10-06 14:48:55 UTC
I have checked this issue with Xamarin.Forms 1.5.1-pre1 and its working fine.

Hence closing this issue.