wpf WPF自定义控件生命周期图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14761869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:24:47  来源:igfitidea点击:

WPF Custom Control Lifecycle Diagram?

wpfevent-handlingwpf-controls

提问by Jason Frank

I am looking for a diagram that will serve as a reference to show the order of events and overrides that get called for WPF custom controls that derive from common classes such as FrameworkElement, UIElement, Control, ContentControl, Decorator, etc.

我正在寻找一个图表,该图表将作为参考,以显示为从常见类(如 FrameworkElement、UIElement、Control、ContentControl、Decorator 等)派生的 WPF 自定义控件调用的事件和覆盖的顺序。

I am particularly interested in the events and overrides dealing with the process of the control becoming visible and ready to interact with. In other words, I'm not as interested in this diagram showing the events that could deal with the user interacting with the control after it is ready to be interacted with, such as things like MouseLeftButtonDown.

我对处理控件变得可见并准备与之交互的过程的事件和覆盖特别感兴趣。换句话说,我对这个图表不感兴趣,该图表显示了在控件准备好与之交互后可以处理用户与控件交互的事件,例如像 MouseLeftButtonDown 之类的东西。

What I am looking for is something like this diagram(diagrams like these explain the lifecyle of a WPF windowor application) but for custom controls.

我正在寻找的是类似于这个图表(这些图表解释了 WPF窗口应用程序的生命周期),但用于自定义控件。

I've also seen this page, which, in my opinion makes it seem too simplistic. That page seems to basically say that there's mostly just the Initialized, Loaded, and Unloadedevents. To illustrate, a few of the generalevents/overrides I've needed to make use of in custom controls include:

我也看过这个页面,在我看来,它看起来太简单了。该网页似乎基本上说,有大多只是InitializedLoadedUnloaded事件。为了说明,我需要在自定义控件中使用的一些常规事件/覆盖包括:

  • Constructor
  • Loaded
  • UnLoaded
  • IsVisibleChanged
  • OnApplyTemplate
  • 构造函数
  • Loaded
  • UnLoaded
  • IsVisibleChanged
  • OnApplyTemplate

Along with slightly more specialized overrides like Popup's OnOpenedand Expander's OnExpanded, etc.

以及稍微更专业的覆盖,如 PopupOnOpened和 ExpanderOnExpanded等。

The problem I am having is that I forget whenI can do things like VisualStateManager.GoToState(...)(OnApplyTemplateis the first real chance I believe). And I also keep discoveringevents that I need to make use of, such as the IsVisibleChanged (this event proved necessary when I needed a control to do a "reload/refresh" state change when the user returns to the Tab that contains my control). So I keep wondering are there yet othersuch events/overrides that I should really be paying attention to for a custom control? This is where a diagram that shows these things in relation to each other will help.

我遇到的问题是我忘记了我什么时候可以做这样的事情VisualStateManager.GoToState(...)(这OnApplyTemplate是我相信的第一个真正的机会)。而且我还不断发现我需要使用的事件,例如 IsVisibleChanged(当我需要一个控件在用户返回包含我的控件的选项卡时执行“重新加载/刷新”状态更改时,此事件被证明是必要的) . 所以我一直想知道还有其他这样的事件/覆盖我真的应该注意自定义控件吗? 这是显示这些事物相互关联的图表将有所帮助的地方。

Considering these types of events/overrides, this Silverlight chartis pretty close to what I'm looking for, but for WPF. Some of the commenters on that post say that WPF's events/overrides are different than Silverlight's. If so, can you please point me to a similar diagram?

考虑到这些类型的事件/覆盖,这个 Silverlight 图表非常接近我正在寻找的东西,但对于 WPF。该帖子的一些评论者说 WPF 的事件/覆盖与 Silverlight 的不同。 如果是这样,你能指点我一个类似的图表吗?

To further illustrate the need for such a diagram, consider the following sequence of events that I just witnessed on a custom control that derives from ContentControl:

为了进一步说明对此类图表的需求,请考虑我刚刚在从 ContentControl 派生的自定义控件上目睹的以下事件序列:

  1. App started up. (My custom control is in a different tab than the intial tab.)
  2. Initializedevent callback called
  3. Loadedevent callback called
  4. (I click on the tab that contains my custom control)
  5. IsVisibleChangedevent callback called
  6. OnApplyTemplateoverride called
  7. Loadedevent callback called
  8. Loadedevent callback called again
  9. Loadedevent callback called third time in a row
  10. (Control is now fully visible and ready to interact with)
  1. 应用程序启动。(我的自定义控件与初始选项卡位于不同的选项卡中。)
  2. Initialized事件回调调用
  3. Loaded事件回调调用
  4. (我单击包含我的自定义控件的选项卡)
  5. IsVisibleChanged事件回调调用
  6. OnApplyTemplate覆盖调用
  7. Loaded事件回调调用
  8. Loaded事件回调再次调用
  9. Loaded连续第三次调用事件回调
  10. (控制现在完全可见并准备好与之交互)

回答by bitbonk

I doubt such a comprehensive diagram exists. How about you just make it yourself? Override all the methods and/or events your are interested in and put a Trace.WriteLine(new StackFrame(1).GetMethod().Name);in each override. Your output will tell you in which order they were called.

我怀疑存在这样一个全面的图表。还是自己做吧?覆盖您感兴趣的所有方法和/或事件,并Trace.WriteLine(new StackFrame(1).GetMethod().Name);在每个覆盖中放置一个。您的输出将告诉您它们被调用的顺序。

回答by stuart wilson

  1. Initialised is called only once and first
  2. OnApplyTemplate is called second, and whenever the template changes
  3. Loaded is called when the control is ready to be displayed and whenever certain containers reload their contents (notably swapping between TabItems)
  4. IsVisibleChanged is called whenever the visibility changes, and after loaded
  1. Initialised 只被调用一次,并且第一次被调用
  2. OnApplyTemplate 被第二次调用,每当模板改变时
  3. 当控件准备好显示以及某些容器重新加载它们的内容(特别是在 TabItems 之间交换)时调用 Loaded
  4. IsVisibleChanged 在可见性改变时调用,加载后调用

Unloaded is not normally called unless you are in a TabItem and you swap to another one.

除非您在 TabItem 中并切换到另一个 TabItem,否则通常不会调用 Unloaded。

The loaded/unloaded events are not called when you would expect unfortunatly, and this makes it tricky to tell when your control should dispose of it's resources. I have also never managed to find a list of the containers that unload and reload their contents.

不幸的是,加载/卸载事件不会在您期望的时候被调用,这使得判断您的控件何时应该处理它的资源变得很棘手。我也从未设法找到卸载和重新加载其内容的容器列表。