Updating to Forms 2.0.0 throws Layout cycle detected. Layout could not complete exception

HarikrishnanHarikrishnan USMember ✭✭
edited November 2015 in Xamarin.Forms

Updating to Forms 2.0.0 throws Layout cycle detected. Layout could not complete exception in WindowsPhone and WinRT. In Android and iOS, I dont experience any crash, but it makes a strange behavior to my app as there are lot of cyclic layout calls.

My application is basically a grid view that arranges data objects in rows and columns. I have applied Virtualization concept in my application to reuse the rows and columns upon scrolling. This was working fine till the Forms version. ** But after updating to 2.0.0 am facing a lot of issues in my application as too many layout calls happens in my application.

Basically my idea is to have a ScrollView which has a Container derived from Layout<View> as content. My container contains a lot of row which is nothing but customized Layout<View>. Each row holds number of cells(Labels) which are derived from ContentView.

I will bind a collection of objects to my application as input, say a List of 100 objects. If the view contains only ten rows then I will take the first 10 objects from the collection and set each object as BindingContext to each row that is loaded in view. So each row will have an object from the collection as BindingContext. So obviously all the labels inside the row will have the same object as binding Context. And I have binded the TextProperty of each label to a property name in the object(BindingContext) to get the text displayed correctly.

Upon scrolling I will just change the BindingContext of the row (which in turn for all labels inside the row) that goes out of view and will manually rearrange that row to the bottom of the view to get the maximum possible performance. This was working fine till But after updating to version 2.0.0 when I change the BindingContext of the row that goes out of view, an Invalidate measure call occurs from the root view of my application for binding context change in each cell(label) in my row which results in a cyclic layout pass.

On Checking this deeper, I noticed that Xamarin has made an Invalidate Call on TextProperty changed of Label(something like Xamarin.Forms.InvalidationTrigger trigger = RendererReady" I see in call stack) when its BindingContext is changed. This Invalidate calls makes call to the parent of label and its parent recursively, may be to refresh the text change in view. But in my case this totally breaks my application as there will be 40 to 50 labels in view in my case and for each label the invalidate measure makes a root layout pass resulting in Layout Cycle detected exception.

If this Invalidation is mandatory for any purpose, then, It would be more appropriate if Xamarin could give an option to Skip this Invalidation when BindingContext is changed for a view. I guess we already have two such overrides "ShouldInvalidateOnChildAdded and ShouldInvalidateOnChildRemoved". Like that it will be fine if another override "ShouldInvalidateOnBindingContextChanged" is introducted to skip invalidation when BindingContext is changed for a view. This is just my suggestion.

Any kind of fix for this is much appreciable and is mandatory as currently am damn stuck in running my application with latest forms version. Am totally frustrated because of this as I have no clue what to do to overcome this problem. Is there any workaround for this? And when I get this fixed permanently. I hope Xamarin can address this issue with "High Priority" and get me this fixed ASAP.

If you need any other details for fixing this am happy to give it. All I need is to get this issue fixed ASAP.


  • arividararividar ISUniversity ✭✭

    No response to this yet? Have the same issue.
    This seems to be reported in Bugzilla here: https://bugzilla.xamarin.com/show_bug.cgi?id=35026

  • HrafnLoftssonHrafnLoftsson ISMember ✭✭

    @adamkemp: Tagging you here Adam, because you have been so helpful in the past!

    This issue, "Layout cycle detected" (on WinPhone), is currently preventing us from upgrading to Forms 2.0 (it is not a problem in Forms 1.5).

    Do you know if this issue is currently being fixed at Xamarin?
    Are you aware of any workaround?

  • HarikrishnanHarikrishnan USMember ✭✭

    It seems not.

    I have filed a bug report on this. Xamarin has confirmed it as a defect. However there is no information after that.
    See here: https://bugzilla.xamarin.com/show_bug.cgi?id=36328

    Am too eager to know if there is any workaround to avoid this issue.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I know nothing about Windows Phone, and I generally stay away from posts about upgrade issues because they're usually just bugs that should be reported.

  • Valdi.6986Valdi.6986 USMember

    amazing that there's no response from Xamarin about this critical issue, they haven't even replied or changed the status of the bugzilla bug 35026
    It would be good to know if Xamarin is working on a fix or give us an ETA.
    For me it just seems like there's no commitment from Xamarin when it comes to the Windows mobile platform.

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Please look at my comment in this thread; http://forums.xamarin.com/discussion/comment/167460/#Comment_167460

    @GeraldVersluis said:

    Happened to me on Xamarin.Forms 2.0

    Also be sure to set a MinimumHeightRequest on your StackLayouts and Grids, especially when they are in (View)Cells that worked for me!

  • Valdi.6986Valdi.6986 USMember

    Hi Gerald Versluis

    I've already tried that and most of the tips/tricks provided in that post without any luck. The funny thing is that my data template only consists of a single grid and few labels and ItemsSource of less than 30, and it worked fine in previous versions so something must have changed in the latest update of Xamarin.Forms

  • MarcoTMarcoT USMember ✭✭

    I have a relativeLayout inside a scrollview...
    sometimes I receive the same error (depends on how many objects I have inside the relative layout)!

    I have another problem maybe related to this...sometimes, the background of the scrollview/RelativeLayout is black...I think that this happens if the relative layout is quite big!

    Of course, because of this error, I can't release the app for windows phone!
    Please help us to fix this error!


  • VaibhavMVaibhavM USMember ✭✭

    I am also facing the same issue. Any progress on this ? :(

  • AndreasBrostenAndreasBrosten USMember ✭✭

    I'm also facing the same problem. Windows Phone really doesn't seem like a high priority for Xamarin.
    Forms 2.0.1 didn't fix it either... =(

  • MarcoTMarcoT USMember ✭✭

    I still have the same problem.

    At the end, I decided to rewrite my UI...it seems that the error happens only when there are more than a certain number of items attached to the layout.

    I hope they can fix it ASAP

  • BryanHunterXamBryanHunterXam USXamarin Team Xamurai

    The team has been looking into this one since it was reported. The root cause turned out tricky to determine. We now think we've identified what's up, and we have a fix that we’re applying to a reproduction project. More news soon…

    Gory details:
    In 2.0 there were many improvements to layout: many layout bugs were squashed and layout performance was improved. For Windows the snag comes with the way layout cycles are detected: Windows bumps a counter when certain methods are called, and when some threshold is met Windows assumes a cycle and boom.

    In a cruel twist, our 2.0 improvements around those 'counted methods' are now much faster (and the wall time is better), but for correctness we make more (cheaper, but more) calls into them. I guess Window's cycle detection didn't expect us to be so darn good ;) Anyway... the cycle bump happens when some values are read (not mutated) so we believe we can safely memoize two specific calls and avoid the problem.

  • BryanHunterXamBryanHunterXam USXamarin Team Xamurai

    It looks like today's memoization patch fixes the problem. The fix will appear in 2.1.0-pre3 (unless testing finds something nasty).

    For the brave-of-heart, you can test this patch by using https://www.nuget.org/packages/Xamarin.Forms/ and then blowing away any “Xamarin.Forms.Core.dll” from Nuget with the "Xamarin.Forms.Core.dll" in the .zip attached to this post.

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    I'll add that this should help fix the problem, it wont stop this from happening on deep enough complex enough trees.

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    edited January 2016

    @BryanHunter.xam said:
    It looks like today's memoization patch fixes the problem. The fix will appear in 2.1.0-pre3 (unless testing finds something nasty).

    For the brave-of-heart, you can test this patch by using https://www.nuget.org/packages/Xamarin.Forms/ and then blowing away any “Xamarin.Forms.Core.dll” from Nuget with the "Xamarin.Forms.Core.dll" in the .zip attached to this post.

    Being the brave-hearted guy I am I've tried it, and guess what.. It actually works! Thanks a lot! :smile:

    edit the error still pops up when I go to landscape orientation. But I can live with that for now

  • I tried the attached Xamarin.Forms.Core.dll, the LayoutCycleException happens now less frequently. But especially when navigating from a ListView Item Detail page back to the ListView the exception still happens from time to time.

  • HarikrishnanHarikrishnan USMember ✭✭

    Very sorry, but I must say that the Xamarin.Forms.Core custom dll along with 2.1.0-pre2 does not fix the issue in WndowsPhone.

    Further it creates many other problems. When I run our application with the Android it loads fine, but when scrolling the app crashes with the below stack trace.

    0x25 in System.Diagnostics.Debugger.Mono_UnhandledException_internal
    0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/data/lanes/2512/d3008455/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122,4
    0x39 in object.98aae1f9-3d90-4dc7-b19b-370a74af7946
    0x3A in Xamarin.Forms.Platform.Android.InnerGestureListener.Android.Views.GestureDetector.IOnGestureListener.OnScroll
    0x20 in Android.Views.GestureDetector.IOnGestureListenerInvoker.n_OnScroll_Landroid_view_MotionEvent_Landroid_view_MotionEvent_FF at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.GestureDetector.cs:594,5
    0x29 in object.98aae1f9-3d90-4dc7-b19b-370a74af7946
    0x52 in Android.Runtime.JNIEnv.CallBooleanMethod at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:232,4
    0x66 in Android.Views.GestureDetector.OnTouchEvent at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.GestureDetector.cs:1661,6
    0x47 in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.View>.Android.Views.View.IOnTouchListener.OnTouch
    0x1C in Android.Views.View.IOnTouchListenerInvoker.n_OnTouch_Landroid_view_View_Landroid_view_MotionEvent_ at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:3547,5
    0x1D in object.dce2e4f7-89ee-41ae-bbfb-4f952d37ddcb
    0x73 in Android.Runtime.JNIEnv.CallNonvirtualBooleanMethod at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:658,4
    0x93 in Android.Views.View.DispatchTouchEvent at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:14093,6
    0x37 in Xamarin.Forms.Platform.Android.PlatformRenderer.DispatchTouchEvent
    0x13 in Android.Views.View.n_DispatchTouchEvent_Landroid_view_MotionEvent_ at /Users/builder/data/lanes/2512/d3008455/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:14073,4
    0x17 in object.b6fcd720-4587-4849-989b-48b03e40a03b

    • App only loads in android but crashes when scrolling.
    • Layout issue still persists in WindowsPhone.

    I have not yet tested dlls in iOS as I met with these critical problem in other platforms.

    Can you please check this and revert us back again?

  • AlbertoPuyanaAlbertoPuyana COMember ✭✭

    I have the same problem with 2.1.0-pre3, granted, I have a complex tree (lots of controls), but that same code works perfectly on iOS and Android.
    My problem always happens when navigating back.

  • VaibhavMVaibhavM USMember ✭✭

    I have also upgraded to xamarin forms version 2.1.0-pre3 but still I am facing the same issue for windows

  • DanManningDanManning USMember ✭✭

    I'm running into this problem on Xamarin.Forms and grid controls in a TabbedPage. I read in one of the other threads about this issue that setting MinimumHeightRequest on each grid would fix it. I tried a few different values between 0-20 but still get the same error. Is there some other workaround to solve it?

  • PankajGPankajG USMember ✭✭

    I was getting this error when I was making Async calls in background for populating data on same page. All calls were happening a number of time and updating the same elements (ListView) on the page.

    If you can break the simultaneous updation of ListView by async calls then this problem will get resolve automatically.

Sign In or Register to comment.