In the documentation for Shell it states the following:
In a Shell application, every ContentPage object in a ShellContent object is created during application startup, which can lead to a poor startup experience. However, Shell also allows pages to be created on demand, in response to navigation. This can be accomplished by using the DataTemplate markup extension to convert each ContentPage into a DataTemplate, and then setting the result as the ShellContent.ContentTemplate property value
To test the example provided by the documentation I downloaded the Xaminals example app associated with that same documentation. Sure enough, when starting up, the CatsPage loads and the DogsPage does not.
However, the problem I am having is when switching from one Tab Set to another. It appears that when switching to a different Tab Set, several of the pages in the Tab Set are created, in a similar fashion to what you would get if you did not wrap the page in a DataTemplate. It appears that the pages that do not ‘auto’ load are the ones associated with the first tab set, and perhaps the last page of a Set when that Set is selected.
To test this new problem/theory I modified the Xaminals application slightly by changing the “other” animal tabs as follows in the AppShell.xaml file:
<Tab Title="Other Animals" Route="otheranimals">
<ShellContent Route="monkeys"
Style="{StaticResource MonkeysShell}"
Title="Monkeys"
Icon="monkey.png"
ContentTemplate="{DataTemplate views:MonkeysPage}" />
<ShellContent Route="elephants"
Style="{StaticResource ElephantsShell}"
Title="Elephants"
Icon="elephant.png"
ContentTemplate="{DataTemplate views:ElephantsPage}" />
<ShellContent Route="bears"
Style="{StaticResource BearsShell}"
Title="Bears"
Icon="bear.png"
ContentTemplate="{DataTemplate views:BearsPage}" />
</Tab>
Then, in the constructor of each “XXXXsPage.xaml.cs” page I added
Debug.WriteLine("Monkeys Page Constructor Called!");
Started the app and found that, just like before the initial Tab set only created the first page, and not the DogPages. However, when I traversed to the “Other Animals” Tab Set the following printed in the output window, yet I had not actually navigated to Elephants:
[0:] Monkeys Page Constructor Called!
[0:] Elephants Page Constructor Called!
This simple test raises few questions for me:
1) What occurs during the “convert each ContentPage into a DataTemplate” action?
2) Why does this only appear to work for the initial Tab Set? Is it restricted to application startup only and once the application is started it reverts to loading all pages in the Set?
3) I would have expected this failure to also create the “BearsPage” but it appears that it did not do this. Why not? The "BearsPage" ShellContent is not defined any differently than the "ElephantsPage" ShellContent.
4) I would like to improve my application performance by limiting the initial load of a Shell Tab Set to just the first page, allowing the subsequent pages in the set to be loaded on demand. How can I accomplish this?
5) Is this a bug in Xamarin Shell?
Related Links:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/tabs#efficient-page-loading
https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-xaminals/