I am developing a tabbed Page in Xamarin that consist of 3 tabs ( Tab1= "Home" , Tab2= "Chat" ,Tab3="Menu" )
When the App starts ( MainPage = new TabbedPage() ) it initializes (InitializeComponent();) all the tabs together at first.
My question is that if there is a way to initialize each component alone whenever the tab is pressed?
In my experience, what takes most of the time isn't loading the XAML, its a combination of "measure and layout", then "loading data" (e.g. the items of a list).
This example shows how to defer "measure and layout". It is as simple as setting
IsVisible="False"on any XAML you want to defer. Then when it is needed, setIsVisible="True". I show the simplest case, where there is no data binding.Deferring data loading depends on your data. The simplest technique is to start all lists (
ListView,CollectionView) with ZERO ELEMENTS. When the data is needed, add the elements. I do not show that below. See How to load data when a tab is clicked.NOTE: ON Android,
InitializeComponentcan be sped up further if you have Visual Studio 2019 Enterprise. In yourMyProjectName.Androidproject, in Project Properties / Android Options (for Configuration"Release") / Code Generation and Runtime, check "AOT Compilation". and "Enable Startup Tracing". You may want to also check "Use LLVM Optimiziing Compiler". These options may increase app bundle size. Test load time on an actual device - emulator may give misleading results as to what combination loads fastest. (These options might give very slow load on an Android emulator.)(Unfortunately, my experience is that Xamarin Forms Android initial app load is still noticeably slower than loading a Xamarin (Native) Android app (I think this is time loading the Xamarin Forms library itself). I recommend creating a native Activity to load immediately, to have a page to show while XF loads. That's a different topic.)
TabbedPage1.xaml:
TabbedPage1.xaml.cs:
SlowPage.xaml:
SlowPage.xaml.cs:
StartPage.xaml:
StartPage.xaml.cs: