I'm building a custom control in Silverlight by deriving from ContentControl and doing some special formatting to put a dropshadow behind the contents.
I've nearly got it working but have recently ran into a bizarre error. It works fine if it contains anything besides a Border, or a Grid/Stackpanel/etc that does not have an explicitly defined height and width.
I get a JavaScript error in IE, and the text says:
Runtime Error 4008... Layout Cycle Detected... Layout Could Not Complete.
If I specify a height and width on the contained grid/stackpanel/etc it works fine.
There is a ton on the web about this error when too many textboxes are used (over 250), but I'm able to reproduce my error with a single button in a grid.
I have no textboxes at all on the page. The error has to do with a detected infinite loop. I set a few breakpoints in the code and it seems that the "SizeChanged" event is getting called a lot during rendering, and each time the height/width increments by 10.
I'm assuming that setting a default height/width causes it to skip this incrementing of the number, but I have no idea why this error is happening.
Has anyone ran into this or have any ideas?
A common cause is handling
SizeChanged
and then in the handler doing something that affects the size of the element. Sometimes this is not obvious - it could be modifying child elements which affect the size of their container for instance.There is a good blog post on this error here.
Bascially what can happen is you're changing some size in a
MeasureOverride
somewhere which causes another measure, which changes the size, which causes a measure and so on. I ran into this once before and fixed it by removing any code that caused a layout update or triggered a layout update during the layout cycle.Update: Since the blog post is gone, quoting it here in full:
I had the same problem and what i did was put all the layout updates (size changes) in a "invoke" delegate en invoked later, it stops crashing but you there is a good change it's stuck in a loop
I had the same problem but it only occurred extremely rarely, my code hasn't changed for years and only recently someone managed to experience it.
I had a TextBlock inside a LongListSelector DataSource and its FontSize was set to 21. Changing the FontSize to ANY other value fixed the problem for me...
My LongListSelectors is inside a ScrollViewer.
Fix:
1.If you are using LongListSelector inside ScrollViewer, better remove that. I was facing the same problem and my LongListSelector was inside ScrollViewer. During ItemRealized event, was getting this error.
2.Don't use updatelayout() inside itemrealized..I was using something like
Simply use ScrollTo
3.If you are using image inside longlistselector, make sure to set the height and width of the image.