本文介绍了WPF控制:哪里是“ OnLoaded”?虚拟功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WinForm的控件中,有一个OnLoaded虚拟函数,但是WPF控件中似乎缺少此函数。我发现此功能在某些情况下非常有用。例如,在控件完全初始化之后,我可以在这里做一些事情。在WPF控件中,有一个OnInitialized虚拟函数,但是从InitializeComponent函数调用此函数为时过早,并且不允许设置派生类。有什么理由在WPF中不具有此功能吗?还是我错过了任何事情?

In WinForm's control, there is an OnLoaded virtual function, but this seems to be missing in WPF control. I found this function very useful in some situations. For example, I could do something here after the control is "completely" initialized. In WPF control, there is an OnInitialized virtual function, but this function is called from InitializeComponent function which is too early and it doesn't allow derived class to setup. Is there any reason not to have this function in WPF? Or did I miss anything?

推荐答案

您可以附加到Window对象的Loaded事件并在内部做您想做的事情事件处理程序(假设您正在使用c#):

You can attach to the Loaded event of your Window object and do what you want to do inside the event handler (assuming you are using c#):

public MyWindow() //constructor
{
  this.Loaded += MyWindow_Loaded;
}

private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
  // do your stuff here
}

这篇关于WPF控制:哪里是“ OnLoaded”?虚拟功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:05