2012-08-04 251 views
1

我已经将WindowLoaded事件添加为构造函数中的路由事件处理程序,但不确定这是否是声明/起诉窗口加载的正确方法。WPF中的WindowLoaded事件

this.AddHandler(Window.LoadedEvent, new RoutedEventHandler(WindowLoaded)); 

目前似乎工作,因为我只要一运行该应用程序我得到边框的空窗不只是它的内容。

问题是,如何在WPF中使用WindowLoaded()事件?我期待从VS2010中的顶部下拉框中选择它,但我想必须手动输入它。是对的吗?

干杯, 阿米特

+1

没有名为“WindowLoaded”或“LoadedEvent”的事件。只有Loaded。 – 2012-08-04 18:29:19

回答

1

您可以按照Mark的建议,或者,您可以将加载的事件添加到您的MainWindow.xaml(Visual Studio将生成请按照加载属性中的ctrl +空格为您处理该处理程序):

<Window x:Class="StackOverflowWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Loaded="MainWindow_Loaded"> 
    <!-- your code --> 
</Window> 
3

您既可以在GUI设置Loaded事件:

enter image description here

或控制的代码隐藏:

public MainWindow() 
{ 
    InitializeComponent(); 
    Loaded += MainWindow_Loaded; 
} 

void MainWindow_Loaded(object sender, RoutedEventArgs e) 
{ 

} 
+0

有趣的是,我的属性窗口不显示您的图像!它只显示属性选项卡,并且只有几个属性。 – 2012-08-06 20:23:19

+1

在Visual Studio 2015中,对于我来说,礼貌窗口在名称字段旁边有一个扳手图标和一个闪电图标(没有上面显示的“属性”或“事件”字样)。我花了一秒钟才注意到;以防止有人帮助。 – logicbird 2016-10-01 10:09:59