如何知道何时在 Silverlight 中添加了新的 UIElement
我正在 sliverlight 中设计一个继承自 canvas 的用户控件。 我有必要找出新的 UIElement
何时添加到 MyBase 的 Children 属性,但没有像“ItemAdded”这样的事件。 由于我想为画布的子级设置动画,因此我无法使用 LayoutUpdated 事件(它已被点击一百万次)。
I am designing a user control in sliverlight that inherits from canvas. It is necessary for me to find out when a new UIElement
is added to Children property of MyBase but there is no event like "ItemAdded". Since I want to animate the children of my canvas, I can not use LayoutUpdated
event (It is hit a million times).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当添加/删除子级画布时,它会自动失效,以便发生测量/排列循环。 因此您不需要显式跟踪添加/删除操作。
相反,您只需要重写 MeasureOverride 方法,然后在该方法内您可以查看 Children 集合并注意发生了什么变化。 这是 Silverlight 比 WPF 更难使用的地方。 您无法像 WPF 一样提供自己的集合来存储子项,也无法在现有的 Children 集合上挂钩事件。
When a child is added/removed the
Canvas
it will automatically be invalidated so that a Measure/Arrange cycle occurs. So you do not need to explicitly track the adding/removing operations.Instead you just need to override the MeasureOverride method and then inside the method you can look at the set of Children and notice what has changed. This is one area that Silverlight is much harder to use than WPF. You cannot provide your own collection for storing children like WPF and cannot hook event on the existing Children collection.
请注意,我没有 Silverlight 经验,但在 WPF 中,您可以在控件上使用 Loaded 和 Initialized 事件。
有关来自 MSDN 的 Mike Hillberg 的博客的详细信息
初始化事件
已加载事件
Note that I have no Silverlight expierence, but in WPF you can use the Loaded and Initialized events on a control.
More info on a blog from Mike Hillberg from MSDN
Initialized Event
Loaded Event