单击选项卡时如何在TabbedPage中加载数据? [英] How to load data in TabbedPage when a tab is clicked?
问题描述
我正在使用TabbedPage
进行标签导航.我所有的Page
类都只有一个空的默认构造函数,我将数据加载到OnAppearing
方法中.我有5个标签.当我单击第二个选项卡时,也会调用第三,第四和第五页的OnAppearing
方法.
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?
推荐答案
解决方案:
您可以在方法OnCurrentPageChanged
中获取currentPage的索引,如果索引等于1(第二页),请使用messagecenter
将消息发送到该页面.请参考以下代码.
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");
}
}
在第二页中.将将数据从onAppearing加载到构造函数中的代码
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");
});
}
这篇关于单击选项卡时如何在TabbedPage中加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!