为什么要运行的方法code。通过XAML Window.Loaded叫什么名字? [英] Why to run code in method called by XAML Window.Loaded?
问题描述
我看到创建一个方法code例如 Window_Loaded()
这是由XAML的加载窗口之称事件:
I saw a code example that creates a method Window_Loaded()
which is called by XAML's "Window Loaded" event:
<Window x:Class="TestModuleLoader.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
...
</Grid>
</Window>
但在code背后,code在构造和 Window_Loaded()
方法都工作:
using System.Windows;
namespace TestModuleLoader
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//what advantages do I have running code here?
}
}
}
是否有任何优势,这样做呢?
Are there any advantages to doing this?
有一个窗口负载周期在ASP.NET怎么回事这是有益的了解,即方法,如 preRender()
, PostRender()
,等?
Is there a "Window Load Cycle" as in ASP.NET going on here that is helpful to know about, i.e. methods such as PreRender()
, PostRender()
, etc?
推荐答案
是的,有一个WPF控件类似的生命周期,就像在ASP.NET。 WPF控制的生命周期是简单不过,因为它基本上consits一个初始化,加载,和卸载事件的(按该顺序)。参见:
Yes, there is a similar life cycle for WPF controls, just like in ASP.NET. The life cycle of WPF controls is simpler though, as it basically consits of an initialized, loaded, and unloaded event (in that order). See:
<一个href=\"http://msdn.microsoft.com/en-us/library/ms754221.aspx\">http://msdn.microsoft.com/en-us/library/ms754221.aspx
和迈克Hillberg拥有一支优秀的文章展示initalized并加载事件之间的区别:
and Mike Hillberg has an excellent article demonstrating the difference between the initalized and loaded events:
<一个href=\"http://blogs.msdn.com/mikehillberg/archive/2006/09/19/LoadedVsInitialized.aspx\">http://blogs.msdn.com/mikehillberg/archive/2006/09/19/LoadedVsInitialized.aspx
这篇关于为什么要运行的方法code。通过XAML Window.Loaded叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!