Xamarin Forms Tabbed Page Load Data when tab is Select

Xamarin Forms Tabbed Page Load Data when tab is Select

我在 Xamarin Forms 中有一个像这样的 TabbedPage :

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:AppName.Views.Main"
            xmlns:Search="clr-namespace:AppName.Views.Search"
             x:Class="AppName.Views.Main.BottomNavigationPage"
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
             android:TabbedPage.ToolbarPlacement="Bottom"
             android:TabbedPage.BarSelectedItemColor="{DynamicResource PrimaryColor}"
             android:TabbedPage.BarItemColor="{DynamicResource Gray-600}"
             android:TabbedPage.IsSwipePagingEnabled="False"
             BarBackgroundColor="White"
             NavigationPage.HasNavigationBar="False">
    <TabbedPage.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </TabbedPage.Resources>
    
    <local:HomePage Title="">
        <local:HomePage.IconImageSource>
            <FontImageSource FontFamily="{StaticResource FASolid}"
                                 Glyph="&#xf015;"
                                 Size="10"/>
        </local:HomePage.IconImageSource>
    </local:HomePage>
    <Search:ExplorePage Title="">
        <Search:ExplorePage.IconImageSource>
            <FontImageSource FontFamily="{StaticResource FASolid}"
                                 Glyph="&#xf002;"
                                 Size="10"/>
        </Search:ExplorePage.IconImageSource>
    </Search:ExplorePage>
    <local:PhotosPage Title="">
        <local:PhotosPage.IconImageSource>
            <FontImageSource FontFamily="{StaticResource FASolid}"
                                 Glyph="&#xf0fe;"
                                 Size="10"/>
        </local:PhotosPage.IconImageSource>
    </local:PhotosPage>
    <local:SettingsPage Title="">
        <local:SettingsPage.IconImageSource>
            <FontImageSource FontFamily="{StaticResource FASolid}"
                                 Glyph="&#xf004;"
                                 Size="10"/>
        </local:SettingsPage.IconImageSource>
    </local:SettingsPage>
</TabbedPage>

每个页面都有在 InitializeComponent() 之后的代码中分配给它的 ViewModel,每个 ViewModel 从 Rest 获取数据 API

当 App 运行ning 和 SplashScreen 之后所有 ViewModel 执行并获取数据

What can I do to 运行 each ViewModel when tab is select not before selection ?

TabbedPage 中有一个名为“OnCurrentPageChanged”的重写方法,它调用选项卡更改。我们可以使用 x:Name 属性在代码后面访问 xaml 的元素。

在 Xaml 中,应用 x:Name 属性值。

<local:HomePage x:Name="Home">
<local:ExplorePage x:Name="Explore"> //use same for others pages

在后面的代码中重写下面的方法。

protected override void OnCurrentPageChanged()
        {
            base.OnCurrentPageChanged();
            if(CurrentPage is HomePage)
            {
                Debug.WriteLine("Home Page");
                var viewModel = Home.BindingContext as HomeViewModel;
                viewModel.CallMethodToLoadData();
            }else if(CurrentPage is ExplorePage)
            {
                Debug.WriteLine("Explore Page");
                var viewModel = Explore.BindingContext as ExploreViewModel;
                viewModel.CallMethodToLoadData();
            }
            // Same for other pages
        }