Yellow
Yellow

Reputation: 3966

How can I explicitly load XAML before it's visible?

I have a TabControl with several tabs. The first and main one is very simple one and takes virtually no time at all to load. The second one on the other hand is a representation of hundreds of complex objects, and takes about 1-3 seconds. The application fires up rapidly, but as soon as you click on the second tab, the application freezes for a short while, loading the Tab Control.

This is a quick illustration of the XAML code:

<Window x:Class="MyProgramme.MyMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
         <TabControl VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="0">
            <TabItem Header="Simple View" >
                <!-- Main view. Loads instantly. -->
                <ContentPresenter x:Name="m_simple"/>                    
            </TabItem>
            <TabItem Header="Heavy View" > 
                <!-- Takes about 1-3 seconds to load-->
                <ContentPresenter x:Name="m_heavy"/>
            </TabItem>
         </TabControl>
    </Grid>
</Window>

Obviously, this waiting is not very user friendly, and I would like a way to explicitly load the second tab whenever it's convenient, which will be either at start-up, or whenever the UI thread is not too busy and can take the load. Is there a way of loading this tab without stalling the user's work flow?

Upvotes: 0

Views: 528

Answers (1)

toadflakz
toadflakz

Reputation: 7934

If you are loading the data for the tab in the constructor, that would be the reason for the freeze.

Rather use the Window's Loaded event instead and if possible, try kick off the heavy load in a background thread. Loaded is triggered once the UI is drawn leading to a better user experience as the UI itself does not appear to freeze.

You should also consider a BusyIndicator or ProgressBar for the second tab while the background load is running to indicate to the user that a long running process is happening.

Upvotes: 1

Related Questions