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

前端 未结 1 1267
慢半拍i
慢半拍i 2021-01-23 17:48

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<

相关标签:
1条回答
  • 2021-01-23 18:23

    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");
      });
    
    } 
    
    0 讨论(0)
提交回复
热议问题