gpt4 book ai didi

使用自定义控件时 Silverlight 错误 "Layout Cycle Detected Layout could not complete"

转载 作者:行者123 更新时间:2023-12-03 22:15:58 26 4
gpt4 key购买 nike

我正在通过从 ContentControl 派生并执行一些特殊格式以在内容后面放置阴影来在 Silverlight 中构建自定义控件。

我几乎让它工作了,但最近遇到了一个奇怪的错误。如果它包含除边框或没有明确定义的高度和宽度的网格/堆栈面板/等之外的任何内容,则它可以正常工作。

我在 IE 中收到一个 JavaScript 错误,文本显示:

Runtime Error 4008... Layout Cycle Detected... Layout Could Not Complete.



如果我在包含的网格/堆栈面板/等上指定高度和宽度,它工作正常。

当使用太多文本框(超过 250 个)时,网络上有很多关于此错误的信息,但我可以使用网格中的单个按钮重现我的错误。

我在页面上根本没有文本框。该错误与检测到的无限循环有关。我在代码中设置了一些断点,看起来“SizeChanged”事件在渲染过程中被调用了很多,并且每次高度/宽度增加 10。

我假设设置默认高度/宽度会导致它跳过这个数字的递增,但我不知道为什么会发生这个错误。

有没有人遇到过这个或有任何想法?

最佳答案

有一个good blog post on this error here .
基本上可能发生的事情是你在 MeasureOverride 中改变了一些大小。某处导致另一个度量,改变大小,导致度量等等。我之前遇到过这个问题,并通过删除在布局周期中导致布局更新或触发布局更新的任何代码来修复它。
更新:由于博客文章已经消失,请在此处完整引用:

Continuing my series of gotchas for Silverlight 2, I wanted to talk about a common error that people are seeing. This error is something new that you might see when moving code from Beta 2 to the Release Candidate or later. In Beta 2, if the layout engine detected a cycle, it didn't throw any errors; as I understand it, the layout was just aborted. But with post Beta2 bits, an error is thrown.

The error you'll get will specify "Layout Cycle Detected" as the message. This error message is very accurate--the layout engine detected a cycle within your layout; or another way to say it, you have an infinite loop in your layout.

The biggest culprit that leads to this error is code within the LayoutUpdated event handler. If your LayoutUpdated event handler does anything to alter the layout of your control, then that will cause the LayoutUpdated event to fire again, and again, and again... :-)

Sometimes you need to have layout altering code within this event handler though, so what is one to do?

First, you should consider whether you really need the layout changes to occur on every call to LayoutUpdated. Would it suffice to handle the Loaded event as well as the Application.Current.Host.Content.Resized event. Between these two events, you'll get notified when the control is loaded into the visual tree, and you'll get notified any time the host is resized, which could cause you to need to change your layout again. Scenarios like modal dialogs should fall into this category.

Second, if you really do need to use LayoutUpdated, you might just need to put some conditions around your layout changes. For instance, if you are calculating a new width and height for your control, before you actually set the width and height, check to make sure the current values differ from what you calculated. This will allow the first LayoutUpdated event to resize your control, which triggers another LayoutUpdated event, but that event will recognize that there's no work to do, and the cycle will end.

These same rules will apply when you're handling the SizeChanged event, or if you're doing any other overrides on the layout of your control.

关于使用自定义控件时 Silverlight 错误 "Layout Cycle Detected Layout could not complete",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/636245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com