How to detect the first showing of a WPF window?

I was wondering how to correctly determine when WPF windows were first shown?

+5
source share
3 answers

There is an event with a name Loadedthat you can use to determine when your window will be ready.

From MSDN

Occurs when an item is laid out, displayed, and ready for interaction.

install handler in xaml

<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.FELoaded"
Loaded="OnLoad"
Name="root">
</StackPanel>

add code back

void OnLoad(object sender, RoutedEventArgs e)
{
    Button b1 = new Button();
    b1.Content = "New Button";
    root.Children.Add(b1);
    b1.Height = 25;
    b1.Width = 200;
    b1.HorizontalAlignment = HorizontalAlignment.Left;
}
+7
source

I would suggest making the bool flag and checking it, and in the constructor set it to true

bool FirstTime = true;

void OnLoad(object sender, RoutedEventArgs e)
{
     if (FirstTime)
     {
          FirstTime = false;
          //do your stuff first-time
     }
     else
     {
           //do your stuff for other
     }
}
-1
source

.

Loaded Initialized

, Loaded .
, OnLoaded.

, Stack Panel TabItem, , .

-1
source

All Articles