How to load data in TabbedPage when a tab is clicked?

北战南征 提交于 2019-12-11 08:04:12

问题


I am using TabbedPage for navigation with tabs. All my Page classes have just an empty default constructor and I load my data in the OnAppearing method. I have 5 tabs. As soon as I click on the second tab, the OnAppearing methods of the 3rd, 4th and 5th pages are also called.

How do I ensure that the data is only loaded when I click on the tab?


回答1:


Solution:

You can get the index of currentPage in method OnCurrentPageChanged And if the index equals 1(second page) , use the messagecenter to send message to the page.Refer the following code .

in Tabbed Page

protected override void OnCurrentPageChanged()
{
   base.OnCurrentPageChanged();

   int index = Children.IndexOf(CurrentPage);

   if (index == 1)
   {
      MessagingCenter.Send<Object>(this, "click_second_tab");
   }

   else if (index == 2)
   {
      MessagingCenter.Send<Object>(this, "click_third_tab");
   }


} 

in the second page .Move the code that load data from onAppearing to the constructor

public MyPage1()
{

  //...
  MessagingCenter.Subscribe<Object>(this, "click_second_tab", (obj) =>
  {
     //load your data here

      Console.WriteLine("11111");
  });

} 


来源:https://stackoverflow.com/questions/53968876/how-to-load-data-in-tabbedpage-when-a-tab-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!