Window.Loaded和Window.ContentRendered事件有什么区别? [英] What's the difference between the Window.Loaded and Window.ContentRendered events

查看:1408
本文介绍了Window.Loaded和Window.ContentRendered事件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中的 Window.Loaded Window.ContentRendered 事件之间有什么区别?是否首先发送了 ContentRendered 事件?



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


发生在窗口的内容已经呈现。


Window.Loaded 事件的描述此处


当元素布局,呈现和准备交互时发生。


我有一个案例要将窗口的 MaxHeight 设置为正在显示我的窗口的屏幕的工作区域的高度。我应该怎么做?



编辑:



我想我找到了我正在寻找的东西,但我现在更困惑了加载的事件首先发生,然后发生 ContentRendered 事件。在Chris Sells& amp; Ian Griffiths说,加载事件是


在窗口之前提起显示


虽然ContentRendered事件是


当窗口的内容被视觉呈现时提起。


这与MSDN文档中关于加载事件:


元素布局,呈现和准备交互时发生。


现在更令人困惑。

解决方案

我认为这两个事件之间没有差别。为了理解这一点,我创建了一个简单的例子来操纵:



XAML

 < Window x:Class =LoadedAndContentRendered.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Name =MyWindow
Title =MainWindowHeight =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);
}

在这种情况下,消息加载出现在消息 ContentRendered 之后的第一个。这个确认文档中的信息。



一般来说,在WPF中,如果元素为: Loaded / p>


被布局,渲染和准备交互。


由于在WPF中, Window 是相同的元素,但它通常应该是在根面板中排列的内容(例如:网格)。因此,要监视 Window 的内容,并创建一个 ContentRendered 事件。来自MSDN的备注:


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




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

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

它只会工作加载事件。



关于访问窗口中的元素,他们的工作方式相同。我们在 Grid 中的 Window 中创建一个标签。在这两种情况下,我们已成功接收到宽度的访问权限:

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

至于样式模板,在这个阶段他们被成功应用,在这些事件中,我们将能够访问它们。



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

  private void Window_ContentRendered(object sender,EventArgs e)
{
MessageBox.Show(ContentRendered:+ SampleLabel.Width.ToString());

按钮b1 = new Button();
b1.Content =ContentRendered Button;
RootGrid.Children.Add(b1);
b1.Height = 25;
b1.Width = 200;
b1.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Right;


private void Window_Loaded(object sender,RoutedEventArgs e)
{
MessageBox.Show(Loaded:+ SampleLabel.Width.ToString());

按钮b1 = new Button();
b1.Content =加载按钮;
RootGrid.Children.Add(b1);
b1.Height = 25;
b1.Width = 200;
b1.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Left;
}

加载事件,按钮添加到 Grid 立即出现在窗口。在 ContentRendered 事件的情况下,按钮添加到 Grid 其所有内容将出现。因此,如果要在加载窗口之前添加项目或更改,则必须使用加载事件。如果要执行与 Window 的内容相关的操作,例如截屏,您将需要使用事件 ContentRendered


What's the difference between the Window.Loaded and Window.ContentRendered events in WPF? Is the ContentRendered event called first?

The description of the Window.ContentRendered event here just says

Occurs after a window's content has been rendered.

The description of the Window.Loaded event here says

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

I have a case where I want to set the window's MaxHeight to the height of the working area of the screen that is displaying my window. Which event should I do it in?

Edit:

I think I found what I was looking for, but I'm even more confused now. The Loaded event happens first and then the ContentRendered event happens. In the book Programming WPF by Chris Sells & Ian Griffiths, it says that the Loaded event is

Raised just before the window is shown

While the 'ContentRendered` event is

Raised when the window's content is visually rendered.

This contradicts what the MSDN documentation says about the Loaded event:

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

This is even more confusing now.

解决方案

I think there is little difference between the two events. To understand this, I created a simple example to manipulation:

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>

Code behind

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

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

In this case the message Loaded appears the first after the message ContentRendered. This confirms the information in the documentation.

In general, in WPF the Loaded event fires if the element:

is laid out, rendered, and ready for interaction.

Since in WPF the Window is the same element, but it should be generally content that is arranged in a root panel (for example: Grid). Therefore, to monitor the content of the Window and created an ContentRendered event. Remarks from MSDN:

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

That is, if we create a 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" />

It will only works Loaded event.

With regard to access to the elements in the Window, they work the same way. Let's create a Label in the main Grid of Window. In both cases we have successfully received access to 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());
}   

As for the Styles and Templates, at this stage they are successfully applied, and in these events we will be able to access them.

For example, we want to add a 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;
}

In the case of Loaded event, Button to add to Grid immediately at the appearance of the Window. In the case of ContentRendered event, Button to add to Grid after all its content will appear.

Therefore, if you want to add items or changes before load Window you must use the Loaded event. If you want to do the operations associated with the content of Window such as taking screenshots you will need to use an event ContentRendered.

这篇关于Window.Loaded和Window.ContentRendered事件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆