我怎样才能知道一个新的UIElement何时被添加到Silverlight中

时间:2021-12-10 15:09:43

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).

我正在设计一个从canvas继承的sliverlight用户控件。在MyBase的子属性中添加一个新的UIElement是必要的,但是没有像“itemadd”这样的事件。因为我想让画布上的孩子们动起来,所以我不能使用LayoutUpdated事件(它被点击了一百万次)。

2 个解决方案

#1


1  

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.

相反,您只需重写MeasureOverride方法,然后在方法内部,您可以查看一组子元素,并注意发生了什么变化。这是Silverlight比WPF更难使用的一个领域。不能提供自己的集合来存储WPF之类的子集合,也不能将事件挂接到现有的子集合上。

#2


0  

Note that I have no Silverlight expierence, but in WPF you can use the Loaded and Initialized events on a control.

注意,我没有Silverlight expierence,但是在WPF中,您可以在控件上使用已加载和初始化的事件。

More info on a blog from Mike Hillberg from MSDN

更多信息请访问来自MSDN的Mike Hillberg的博客

Initialized Event

初始化事件

The Initialized event typically fires when the properties of an element have all been set. Specifically, FrameworkElement/FrameworkContentElement implement ISupportInitialize, and when the EndInit method of that interface is called, the IsInitialized property is set to true, and the Initialized event is fired.

初始化事件通常在元素的属性都被设置时触发,具体来说,FrameworkElement/FrameworkContentElement实现ISupportInitialize,当调用接口的EndInit方法时,IsInitialized属性被设置为true,事件被触发。

Loaded Event

加载事件

The Loaded event fires when an element is not only initialized, but it is about to be rendered. The motivation for the Loaded event is the typical scenario where you want to do some initialization in your application at load-time.

当一个元素不仅被初始化,而且即将被呈现时,加载的事件就会触发。加载事件的动机是典型的场景,您希望在加载时在应用程序中进行一些初始化。

#1


1  

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.

相反,您只需重写MeasureOverride方法,然后在方法内部,您可以查看一组子元素,并注意发生了什么变化。这是Silverlight比WPF更难使用的一个领域。不能提供自己的集合来存储WPF之类的子集合,也不能将事件挂接到现有的子集合上。

#2


0  

Note that I have no Silverlight expierence, but in WPF you can use the Loaded and Initialized events on a control.

注意,我没有Silverlight expierence,但是在WPF中,您可以在控件上使用已加载和初始化的事件。

More info on a blog from Mike Hillberg from MSDN

更多信息请访问来自MSDN的Mike Hillberg的博客

Initialized Event

初始化事件

The Initialized event typically fires when the properties of an element have all been set. Specifically, FrameworkElement/FrameworkContentElement implement ISupportInitialize, and when the EndInit method of that interface is called, the IsInitialized property is set to true, and the Initialized event is fired.

初始化事件通常在元素的属性都被设置时触发,具体来说,FrameworkElement/FrameworkContentElement实现ISupportInitialize,当调用接口的EndInit方法时,IsInitialized属性被设置为true,事件被触发。

Loaded Event

加载事件

The Loaded event fires when an element is not only initialized, but it is about to be rendered. The motivation for the Loaded event is the typical scenario where you want to do some initialization in your application at load-time.

当一个元素不仅被初始化,而且即将被呈现时,加载的事件就会触发。加载事件的动机是典型的场景,您希望在加载时在应用程序中进行一些初始化。