Window.Loaded和Window.ContentRendered事件之间的区别是什么

Ton*_*ile 52 wpf events window

WPF中的事件Window.LoadedWindow.ContentRendered事件有什么区别?该ContentRendered事件首先被召唤?

这里Window.ContentRendered事件的描述只是说

在窗口的内容呈现后发生.

的的描述Window.Loaded事件在这里说,

元素布局,渲染并准备好进行交互时发生.

我有一个案例,我想将窗口设置MaxHeight为显示我的窗口的屏幕的工作区域的高度.我应该参加哪个活动?

编辑:

我想我找到了我想要的东西,但现在我更加困惑了.该Loaded事件首先发生,然后ContentRendered事件发生.在Chris Sells和Ian Griffiths撰写的WPF编程一书中,它说Loaded事件是

在窗口显示之前升起

虽然'ContentRendered`事件是

在窗口内容以可视方式呈现时引发.

这与MSDN文档中有关该Loaded事件的内容相矛盾:

元素布局,渲染并准备好进行交互时发生.

现在这更令人困惑.

Ana*_*aev 53

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

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>
Run Code Online (Sandbox Code Playgroud)

Code behind

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

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Loaded");
}   
Run Code Online (Sandbox Code Playgroud)

在这种情况下,消息Loaded显示在消息之后的第一个消息ContentRendered.这确认了文档中的信息.

通常,在WPF中,Loaded如果元素:

布局,渲染,准备互动.

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

如果窗口没有内容,则不会引发此事件.

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

<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" />
Run Code Online (Sandbox Code Playgroud)

它只会起作用Loaded.

关于对元素的访问Window,它们以相同的方式工作.让我们创建一个Label主要GridWindow.在这两种情况下,我们都成功地获得了访问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());
}   
Run Code Online (Sandbox Code Playgroud)

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

例如,我们要添加一个Button:

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;
}
Run Code Online (Sandbox Code Playgroud)

Loaded事件的情况下,立即Button添加到Grid外观Window.在ContentRendered事件的情况下,Button添加到Grid所有内容后将出现.

因此,如果要在加载前添加项目或更改,则Window必须使用该Loaded事件.如果您想进行与Window截屏等内容相关的操作,则需要使用事件ContentRendered.


sa_*_*213 44

如果您访问此链接https://msdn.microsoft.com/library/ms748948%28v=vs.100%29.aspx#Window_Lifetime_Events并向下滚动到Window Lifetime Events,它将显示事件订单.

打开:

  1. SourceInitiated
  2. 活性
  3. 加载
  4. ContentRendered

关:

  1. 闭幕
  2. 停用
  3. 关闭


Tre*_*ess 10

如果您正在使用数据绑定,则需要使用ContentRendered事件.

对于下面的代码,引发Loaded事件时Header为NULL.但是,当引发ContentRendered事件时,Header会获取其值.

<MenuItem Header="{Binding NewGame_Name}" Command="{Binding NewGameCommand}" />
Run Code Online (Sandbox Code Playgroud)