TabbedPage 的第一个子页面的 OnAppearing 方法调用了两次

OnAppearing method of the first child page of a TabbedPage called twice

我有一个带有两个子页面 PageA 和 PageB 的 TabbedPage(顺序相同)。 在启动标签页时,PageA 的 OnAppearing 方法被调用两次。我该如何防止这种情况发生?

这是 TabbedPage 的示例代码:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:views="clr-namespace:SampleApp.Pages"
         x:Class="SampleApp.TabbedPage1">
    <views:PageA Title="Page A" />
    <views:PageB Title="Page B" />
</TabbedPage>

好的,我找到了解决方案。由于示例中的PageA是第一个页面,所以只要调用主页面的InitializeComponent,就会初始化索引为0的页面。此外,Xamarin.Forms 似乎将 CurrentPage 属性 设置为子列表中的第一页,导致 OnAppearing 方法被调用两次。为防止这种情况发生,您可以将 TabbedPage 的 CurrentPage 属性 设置为 null。

示例如下:

public TabbedPage1()
{
    InitializeComponent();
    CurrentPage = null;
}