Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think there is little difference between the two events. To understand this, I created a simple example to manipulation:</p> <p><code>XAML</code></p> <pre><code>&lt;Window x:Class="LoadedAndContentRendered.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MyWindow" Title="MainWindow" Height="1000" Width="525" WindowStartupLocation="CenterScreen" ContentRendered="Window_ContentRendered" Loaded="Window_Loaded"&gt; &lt;Grid Name="RootGrid"&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><code>Code behind</code></p> <pre><code>private void Window_ContentRendered(object sender, EventArgs e) { MessageBox.Show("ContentRendered"); } private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("Loaded"); } </code></pre> <p>In this case the message <code>Loaded</code> appears the first after the message <code>ContentRendered</code>. This confirms the information in the documentation.</p> <p>In general, in WPF the <code>Loaded</code> event fires if the element:</p> <blockquote> <p>is laid out, rendered, and ready for interaction.</p> </blockquote> <p>Since in WPF the <code>Window</code> is the same element, but it should be generally content that is arranged in a root panel (for example: <code>Grid</code>). Therefore, to monitor the content of the <code>Window</code> and created an <code>ContentRendered</code> event. Remarks from MSDN:</p> <blockquote> <p>If the window has no content, this event is not raised.</p> </blockquote> <p>That is, if we create a <code>Window</code>:</p> <pre><code>&lt;Window x:Class="LoadedAndContentRendered.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MyWindow" ContentRendered="Window_ContentRendered" Loaded="Window_Loaded" /&gt; </code></pre> <p>It will only works <code>Loaded</code> event. </p> <p>With regard to access to the elements in the <code>Window</code>, they work the same way. Let's create a <code>Label</code> in the main <code>Grid</code> of <code>Window</code>. In both cases we have successfully received access to <code>Width</code>:</p> <pre><code>private void Window_ContentRendered(object sender, EventArgs e) { MessageBox.Show("ContentRendered: " + SampleLabel.Width.ToString()); } private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("Loaded: " + SampleLabel.Width.ToString()); } </code></pre> <p>As for the <code>Styles</code> and <code>Templates</code>, at this stage they are successfully applied, and in these events we will be able to access them. </p> <p>For example, we want to add a <code>Button</code>:</p> <pre><code>private void Window_ContentRendered(object sender, EventArgs e) { MessageBox.Show("ContentRendered: " + SampleLabel.Width.ToString()); Button b1 = new Button(); b1.Content = "ContentRendered Button"; RootGrid.Children.Add(b1); b1.Height = 25; b1.Width = 200; b1.HorizontalAlignment = HorizontalAlignment.Right; } private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("Loaded: " + SampleLabel.Width.ToString()); Button b1 = new Button(); b1.Content = "Loaded Button"; RootGrid.Children.Add(b1); b1.Height = 25; b1.Width = 200; b1.HorizontalAlignment = HorizontalAlignment.Left; } </code></pre> <p>In the case of <code>Loaded</code> event, <code>Button</code> to add to <code>Grid</code> immediately at the appearance of the <code>Window</code>. In the case of <code>ContentRendered</code> event, <code>Button</code> to add to <code>Grid</code> after all its content will appear. </p> <p>Therefore, if you want to add items or changes before load <code>Window</code> you must use the <code>Loaded</code> event. If you want to do the operations associated with the content of <code>Window</code> such as taking screenshots you will need to use an event <code>ContentRendered</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload