单击选项卡时如何在 TabbedPage 中加载数据?
How to load data in TabbedPage when a tab is clicked?
我正在使用 TabbedPage
进行标签导航。我所有的 Page
类 都只有一个空的默认构造函数,我在 OnAppearing
方法中加载数据。我有 5 个标签。当我点击第二个选项卡时,第 3、4 和 5 页的 OnAppearing
方法也被调用。
如何确保只有在点击标签时才加载数据?
解法:
你可以在方法OnCurrentPageChanged
中获取当前页面的索引,如果索引等于1(第二页),使用messagecenter
发送消息到page.Refer下面的代码.
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");
});
}
我正在使用 TabbedPage
进行标签导航。我所有的 Page
类 都只有一个空的默认构造函数,我在 OnAppearing
方法中加载数据。我有 5 个标签。当我点击第二个选项卡时,第 3、4 和 5 页的 OnAppearing
方法也被调用。
如何确保只有在点击标签时才加载数据?
解法:
你可以在方法OnCurrentPageChanged
中获取当前页面的索引,如果索引等于1(第二页),使用messagecenter
发送消息到page.Refer下面的代码.
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");
});
}