gpt4 book ai didi

wpf - Window.Loaded 和 Window.ContentRendered 事件有什么区别

转载 作者:行者123 更新时间:2023-12-03 05:59:08 25 4
gpt4 key购买 nike

WPF 中的 Window.LoadedWindow.ContentRendered 事件有什么区别?是否首先调用 ContentRendered 事件?

Window.ContentRendered 事件的描述 here只是说

Occurs after a window's content has been rendered.

Window.Loaded 事件的描述 here

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

我有一个情况,我想将窗口的 MaxHeight 设置为显示窗口的屏幕工作区域的高度。我应该在哪个事件中进行?

编辑:

我想我找到了我要找的东西,但我现在更困惑了。首先发生 Loaded 事件,然后发生 ContentRendered 事件。在 Chris Sells 和 Ian Griffiths 所著的《Programming WPF》一书中,它说 Loaded 事件是

Raised just before the window is shown

虽然“ContentRendered”事件是

Raised when the window's content is visually rendered.

这与 MSDN 文档中有关 Loaded 事件的说法相矛盾:

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

现在这更加令人困惑。

最佳答案

我认为这两个事件之间没有什么区别。为了理解这一点,我创建了一个简单的操作示例:

XAML

<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">

<Grid Name="RootGrid">
</Grid>
</Window>

隐藏代码

private void Window_ContentRendered(object sender, EventArgs e)
{
MessageBox.Show("ContentRendered");
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
}

在这种情况下,消息Loaded首先出现在消息ContentRendered之后。这证实了文档中的信息。

一般来说,在 WPF 中,如果元素满足以下条件,Loaded 事件就会触发:

is laid out, rendered, and ready for interaction.

由于在 WPF 中,Window 是相同的元素,但它通常应该是排列在根面板中的内容(例如:Grid)。因此,为了监视Window的内容并创建了一个ContentRendered事件。 MSDN 评论:

If the window has no content, this event is not raised.

也就是说,如果我们创建一个窗口:

<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" />

它仅适用于 Loaded 事件。

对于访问Window中的元素,它们的工作方式相同。让我们在Window 的主Grid 中创建一个Label。在这两种情况下,我们都成功获得了对 Width 的访问权限:

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

至于StylesTemplates,在这个阶段它们已经成功应用,在这些事件中我们将能够访问它们。

例如,我们要添加一个按钮:

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

对于 Loaded 事件,ButtonWindow 出现时立即添加到 Grid 。在 ContentRendered 事件的情况下,在其所有内容之后添加到 GridButton 将出现。

因此,如果您想在加载 Window 之前添加项目或更改,则必须使用 Loaded 事件。如果您想要执行与 Window 内容相关的操作(例如截屏),您将需要使用事件 ContentRendered

关于wpf - Window.Loaded 和 Window.ContentRendered 事件有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18452756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com