我想知道检测 WPF 窗口何时首次显示的正确方法是什么?
最佳答案
有一个名为 Loaded
的事件,您可以使用它来确定窗口何时准备就绪。
来自 MSDN
在布局、呈现元素并准备好进行交互时发生。
在 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>
添加代码隐藏
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;
}
关于c# - 如何检测 WPF 窗口的首次显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16106165/