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>
But in the code behind, the code worked in both the constructor and the Window_Loaded()
method:
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?
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?
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:
http://msdn.microsoft.com/en-us/library/ms754221.aspx
and Mike Hillberg has an excellent article demonstrating the difference between the initalized and loaded events:
http://blogs.msdn.com/mikehillberg/archive/2006/09/19/LoadedVsInitialized.aspx