Silverlight 错误“布局周期检测到的布局无法完成” 当使用自定义控件时
我正在 Silverlight 中构建一个自定义控件,方法是从 ContentControl 派生并执行一些特殊的格式设置以在内容后面放置阴影。
我几乎已经让它工作了,但最近遇到了一个奇怪的错误。 如果它包含除边框或网格/堆栈面板/等之外没有明确定义的高度和宽度的任何内容,它就可以正常工作。
我在 IE 中收到 JavaScript 错误,文本显示:
运行时错误 4008...检测到布局周期...布局无法完成。
如果我在包含的网格/堆栈面板/等上指定高度和宽度,它就可以正常工作。
当使用太多文本框(超过 250 个)时,网络上有大量关于此错误的信息,但我可以使用网格中的单个按钮重现我的错误。
我的页面上根本没有文本框。 该错误与检测到的无限循环有关。 我在代码中设置了一些断点,似乎在渲染过程中多次调用“SizeChanged”事件,并且每次高度/宽度都会增加 10。
我假设设置默认高度/宽度会导致它跳过这个数字的递增,但我不知道为什么会发生这个错误。
有没有人遇到过这个或有什么想法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个 关于此错误的好博文在这里。
基本上可能发生的情况是,您在某个地方更改了 MeasureOverride 中的某些大小,这会导致另一个测量,该测量会更改大小,从而导致另一个测量,依此类推。 我之前遇到过这个问题,并通过删除导致布局更新或在布局周期期间触发布局更新的任何代码来修复它。
更新:由于博客文章已消失,因此在此全文引用:
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:
一个常见的原因是处理
SizeChanged
,然后在处理程序中执行一些影响元素大小的操作。 有时这并不明显 - 例如,它可能会修改影响其容器大小的子元素。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.1.如果您在ScrollViewer中使用LongListSelector,最好将其删除。 我面临同样的问题,我的 LongListSelector 位于 ScrollViewer 内。 在 ItemRealized 事件期间,收到此错误。
2.不要在 itemrealized 中使用 updatelayout() ..我使用的是类似
Simple use ScrollTo
3.如果您在 longlistselector 中使用图像,请确保设置图像的高度和宽度。
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.
我遇到了同样的问题,我所做的是将所有布局更新(大小更改)放入稍后调用的“调用”委托中,它停止崩溃,但你有一个很好的更改,它陷入了循环
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
我遇到了同样的问题,但这种情况很少发生,我的代码多年来没有改变,直到最近才有人设法经历它。
我在 LongListSelector 数据源内有一个 TextBlock,其 FontSize 设置为 21。将 FontSize 更改为任何其他值解决了我的问题...
我的 LongListSelectors 在 ScrollViewer 内。
使固定:
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: