什么是加载一个新的WPF / C#窗口何时触发一个事件? [英] What is the last event to fire when loading a new WPF/C# window?

查看:1560
本文介绍了什么是加载一个新的WPF / C#窗口何时触发一个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载preferences窗口,我的申请,我想应用按钮最初被禁用,则当preference更新,应用按钮被再次启用。我有绑定到preferences一些控制数据对象,什么情况是,窗口负载,组合框的事件被触发。是否有保证发生垫底后,一切都稳定无论如何?

I am trying to load a preferences window for my application and I would like the apply button to initially be disabled, then when a preference is updated, the apply button gets enabled again. I have some controls data bound to a preferences object and what happens is that after the window loads, the combobox events get triggered. Is there any event that is guaranteed to happen dead last after everything is stable?

下面是我的code样子(应用按钮窗口加载后始终处于启用状态):

Here is what my code looks like (the apply button is always enabled after the window loads):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    _preferencesData = new PreferencesDataContext();
    LayoutRoot.DataContext = _preferencesData;
    ButtonApply.IsEnabled = false;
}

private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    ButtonApply.IsEnabled = true;
}

是不是还有一个有趣的注意,这仅与文本框和组合框,而不是复选框或单选按钮发生。

Is it also interesting to note that this only happens with textboxes and comboboxes, not checkboxes or radiobuttons.

推荐答案

对于简单的需求最好的解决方案

约瑟夫的答案是迄今为止为您简单的需求的最佳解决方案:只要使用数据绑定,让数据模型处理它

Joseph's answer is the best solution by far for your simple need: Just use data binding and let the data model handle it.

问题答案为提出

有,当你真的需要控制后绝对一切加载完成,所有事件都解雇了更加复杂的场景。有发生垫底没有单一的事件,但它很容易使用分派队列有效地推出自己的。

There are more complex scenarios when you really do need control after absolutely everything has finished loading and all events have fired. There is no single event that occurs "dead last", but it is easy to effectively roll your own using the Dispatcher queue.

这是如何做到这一点:

Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
{
  var x = ComputeSomething(1, 2, 3);
  DoSomething(x, "Test");
}));

一切内部{}当WPF比ContextIdle,其中包括所有的事件处理程序,加载事件,输入事件,渲染,优先级较高的完成一切都将被执行等。

Everything inside the { } will be executed when WPF finishes everything at a higher priority than ContextIdle, which includes all event handlers, loaded events, input events, rendering, etc.

当创建一个窗口,并显示事件序列

按照要求,这里是WPF重大的事件序列被创建并显示一个窗口时:

As requested, here is the sequence of major events in WPF when a window is created and shown:


  1. 构造函数和干将为创建对象,包括PropertyChangedCallback,ValidationCallback等上正在更新的对象和从他们继承的任何对象/ setter方法​​被称为

  1. Constructors and getters/setters are called as objects are created, including PropertyChangedCallback, ValidationCallback, etc on the objects being updated and any objects that inherit from them

由于每个元素被添加到视觉或逻辑树的Intialized事件被触发,这将导致样式和触发器除了你可以定义任何特定的元素初始化时发现应用[注:初始化事件不解雇桑叶在逻辑树,如果没有presentationSource(如窗口)在其根]

As each element gets added to a visual or logical tree its Intialized event is fired, which causes Styles and Triggers to be found applied in addition to any element-specific initialization you may define [note: Initialized event not fired for leaves in a logical tree if there is no PresentationSource (eg Window) at its root]

窗口和所有的非折叠视觉测量,这会导致ApplyTemplate,都在每个控制,这将导致额外的对象树的结构,包括更多的构造函数和getter / setter方法​​

The window and all non-collapsed Visuals on it are Measured, which causes an ApplyTemplate at each Control, which causes additional object tree construction including more constructors and getters/setters

窗口和所有的非折叠视觉排列

The window and all non-collapsed Visuals on it are Arranged

窗口及其后代(逻辑和视觉)收到一个Loaded事件

The window and its descendants (both logical and visual) receive a Loaded event

任何数据绑定,当他们第一套重试失败

Any data bindings that failed when they were first set are retried

窗口和它的后代有机会直观地呈现其内容

The window and its descendants are given an opportunity to render their content visually

步骤1-2创建窗口时完成的,无论它是否被示出。其它步骤一般不会发生,直到所示的窗口,但如果手动触发他们可以更早发生。

Steps 1-2 are done when the Window is created, whether or not it is shown. The other steps generally don't happen until a Window is shown, but they can happen earlier if triggered manually.

这篇关于什么是加载一个新的WPF / C#窗口何时触发一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆