Search code examples
performanceobjectxamarinmaster-detailcreation

Xamarin Form: How to speed up the creation of the content page


Currently I am using MasterDetailPage for my application, I realised the creation of the pages can take very long around a second, which making the application freezes a moment after selecting the menu from the master page.

var TimeA = DateTime.Now;
                var page = (Page)item.GetPage.Invoke();
                System.Diagnostics.Debug.WriteLine("Time A Taken to change = " + DateTime.Now.Subtract(TimeA).TotalMilliseconds);

                var TimeB = DateTime.Now;
                var navigationPage = new NavigationPage(page); 
                navigationPage.BarBackgroundColor = ((NavigationPage)Detail).BarBackgroundColor;
                navigationPage.BarTextColor = ((NavigationPage)Detail).BarTextColor;
                System.Diagnostics.Debug.WriteLine("Time B Taken to change = " + DateTime.Now.Subtract(TimeB).TotalMilliseconds);

                var TimeC = DateTime.Now;
                Detail = navigationPage;
                Detail.Title = item.Title;
                System.Diagnostics.Debug.WriteLine("Time C Taken to change = " + DateTime.Now.Subtract(TimeC).TotalMilliseconds);

I have posted a question before but I thought it was the master detail problem, I have removed that post. Now I realised the problem came from the item.GetPage.Invoke.

Func<Page> GetPage = () => { return new DemoPage(); }

Even if I create using new keyword directly instead of calling GetPage, the time taken still a second.

Time B and Time C both of this take only 50ms to 100ms which acceptable to me. Time A is the worst.

Is there a way to speed it up or doing it asynchronously.

On iOS, the time taken on Time A and Time C can take longer. I would get on 340ms on both time. I am not sure because not much apps running on the iPhone. The iPhone performance on this part is acceptable, at least I don't need to wait a second.


Solution

  • Even though I can't speed up the transition between different navigationPage, I decide to do it in the background task and the background of the MasterDetailPage set as the same as the new page so it won't look awkward.