DeannaD
DeannaD

Reputation: 1535

UWP Event when all page components have loaded?

This seems very simple, but I have googled & googled without success. I'm trying to find the definitive list of page load event ordering. My observations so far are very confusing as the main page's Loaded event completes ahead of some of the user control Loaded events. I think in WPF there's a LoadCompleted event, but I can't find the equivalent.

thanks!

Upvotes: 3

Views: 4399

Answers (1)

DotNetRussell
DotNetRussell

Reputation: 9857

Controls load from the inside out.

Please consider the following example:

My Xaml Page

  <Page
        ... Loaded="Page_Loaded">
    
        <Grid Loaded="Grid_Loaded">
            <StackPanel Loaded="StackPanel_Loaded" Orientation="Vertical">
                <TextBlock Loaded="TextBlock_Loaded" Text="My Text"/>
                <Button Loaded="Button_Loaded" Content="Button"/>
            </StackPanel>
        </Grid>
    </Page>

My behind code

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Frame.Loaded += Frame_Loaded;
        }

        private void Frame_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Frame Loaded");
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Page Loaded");
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Grid Loaded");
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Stack Panel Loaded");
        }

        private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Text Block Loaded");
        }

        private void Button_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Button Loaded");
        }
    }
}

With this setup your output should be Button Loaded, Stack Panel Loaded, Grid Loaded, Page Loaded, Frame Loaded.

and in fact, when I compile and run this I see

Text Block Loaded

Button Loaded

Stack Panel Loaded

Grid Loaded Page Loaded

Frame Loaded

in my output window.

This is because the controls are loading from the inside out. Now you start to run into problems when you're using asynchronous controls. You actually might have two loading events you care about. One says that the UI element is loaded and one says that the content of that UI element is loaded.

Let's use the WebView as an example.

It has multiple completed events.

LoadCompleted Occurs when top-level navigation completes and the content loads into the WebView control or when an error occurs during loading.

FrameDOMContentLoaded Occurs when a frame in the WebView has finished parsing its current HTML content.

FrameNavigationCompleted Occurs when a frame in the WebView has finished loading its content.

It also has the normal Loaded event as well

Loaded Occurs when a FrameworkElement has been constructed and added to the object tree, and is ready for interaction. (Inherited from FrameworkElement)

So the moral of the story is that unless you post your actual code, there's no physical way to tell what is actually going wrong. Every control can have multiple loaded events. Your delay might be caused by anything.

Upvotes: 2

Related Questions