WPF窗口。如何检测第一个显示?

时间:2023-01-25 01:07:57

I was wondering which is the correct way to detect when a WPF windows has been shown for the first time?

我想知道当WPF窗口第一次显示时,哪种方法是正确的?

Thanks in advance.

提前谢谢。

3 个解决方案

#1


7  

There is an event called Loaded that you can use to determine when your window is ready.

您可以使用一个名为Loaded的事件来确定窗口何时准备就绪。

From MSDN

从MSDN

Occurs when the element is laid out, rendered, and ready for interaction.

当元素被放置、呈现并准备进行交互时发生。

set the handler in XAML

在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 the code-behind

添加后台代码

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;
}

#2


-1  

i would suggest to make a bool flag and check it, and in the constructor set it to true

我建议创建一个bool标志并检查它,在构造函数中将其设置为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
     }
}

#3


-1  

Loaded can be called more than once.

加载可以多次调用。

The Loaded event and the Initialized event

加载的事件和初始化事件。

According to my test and the link above, Loaded event can be fired more than once.
So, you need to set a flag in the OnLoaded handler.

根据我的测试和上面的链接,加载事件可以多次触发。因此,需要在OnLoaded处理程序中设置标志。

For example, if Stack Panel is inside TabItem control, loaded will be called every time you step into tab.

例如,如果栈面板在TabItem控件中,每次进入tab时都会调用load。

#1


7  

There is an event called Loaded that you can use to determine when your window is ready.

您可以使用一个名为Loaded的事件来确定窗口何时准备就绪。

From MSDN

从MSDN

Occurs when the element is laid out, rendered, and ready for interaction.

当元素被放置、呈现并准备进行交互时发生。

set the handler in XAML

在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 the code-behind

添加后台代码

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;
}

#2


-1  

i would suggest to make a bool flag and check it, and in the constructor set it to true

我建议创建一个bool标志并检查它,在构造函数中将其设置为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
     }
}

#3


-1  

Loaded can be called more than once.

加载可以多次调用。

The Loaded event and the Initialized event

加载的事件和初始化事件。

According to my test and the link above, Loaded event can be fired more than once.
So, you need to set a flag in the OnLoaded handler.

根据我的测试和上面的链接,加载事件可以多次触发。因此,需要在OnLoaded处理程序中设置标志。

For example, if Stack Panel is inside TabItem control, loaded will be called every time you step into tab.

例如,如果栈面板在TabItem控件中,每次进入tab时都会调用load。