Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择tab时Xamarin表单选项卡式页面加载数据_C#_Xamarin.forms_Viewmodel_Tabbedpage - Fatal编程技术网

C# 选择tab时Xamarin表单选项卡式页面加载数据

C# 选择tab时Xamarin表单选项卡式页面加载数据,c#,xamarin.forms,viewmodel,tabbedpage,C#,Xamarin.forms,Viewmodel,Tabbedpage,我有一个Xamarin格式的标签页,如下所示: <?xml version="1.0" encoding="utf-8" ?> <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

我有一个Xamarin格式的标签页,如下所示:

<?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>

每个页面都有自己的ViewModel,在初始化Component()后在代码隐藏中向其进行签名,每个ViewModel从RESTAPI获取数据

应用程序运行时以及SplashScreen之后,所有ViewModel都会执行并获取数据


当选择选项卡而不是在选择之前选择选项卡时,如何运行每个ViewModel?

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

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
        }
在Xaml中,应用x:Name属性值

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

@jai非常感谢你。如果我喜欢这样做,那么就没有必要在每个页面中分配ViewModel代码隐藏或XAML?我们必须为每个页面设置BindingContext。我的意思是每个页面代码隐藏是的,没有必要。我接受了这个答案。再次感谢你