简体   繁体   English

选择选项卡时,Xamarin 表单选项卡式页面加载数据

[英]Xamarin Forms Tabbed Page Load Data when tab is Select

I Have a TabbedPage in Xamarin Forms like this :我在 Xamarin 表单中有一个 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>

Each page has it's ViewModel that asign to it in code behind after InitializeComponent() and each ViewModel fetch data from Rest API每个页面都有它的 ViewModel,它在 InitializeComponent() 之后的代码中分配给它,每个 ViewModel 从 Rest API 获取数据

When App is running and after SplashScreen all ViewModel execute and fetch data当 App 运行时,在 SplashScreen 之后所有 ViewModel 执行并获取数据

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

There is an override method in TabbedPage named "OnCurrentPageChanged" that call on tab change. TabbedPage 中有一个名为“OnCurrentPageChanged”的覆盖方法,它调用选项卡更改。 We can use x:Name attribute to access the element of xaml in code behind.我们可以在后面的代码中使用 x:Name 属性来访问 xaml 的元素。

In Xaml, Apply x:Name attribute value.在 Xaml 中,应用 x:Name 属性值。

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

Override below method in code behind.在后面的代码中覆盖下面的方法。

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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM