首页 > 解决方案 > 如何从 TapGesture 或按钮按下导航到某个 TabbedPage

问题描述

目标:在选项卡式页面中,我有一个框架和一个框架轻击手势,而在框架轻击手势内选择时,我希望它导航到正确的选项卡式页面,就像选择选项卡式页面一样。

我尝试了各种导航方法,但是它们要么有一个顶部后退按钮,要么不显示底部选项卡,或者不会更新下部菜单导航,或者让我转到主页并选择第一个选项卡。

我有一个带有 5 个标签页的 MainPage。

如何设置我的选项卡式页面的示例:(使用视图:在 xaml 中选择页面)。

<NavigationPage Title="Offers">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_offers.png"/>
                    <On Platform="Android" Value="tab_offers.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:OffersPage />
            </x:Arguments>
        </NavigationPage>

用于在框架内获取点击手势的代码。

<Frame BackgroundColor="Green" HeightRequest="32" WidthRequest="112" Padding="0" CornerRadius="8" HasShadow="False">

  <Frame.GestureRecognizers>
     <TapGestureRecognizer Tapped="TapGestureRecognizer_OfferPage" /
  </Frame.GestureRecognizers>

  <Label Text="Earn Now" TextColor="White" FontSize="14" HorizontalOptions="Center" VerticalOptions="Center" FontAttributes="Bold"/>
</Frame>

目前我使用的导航方法如下:

private async void TapGestureRecognizer_OfferPage(object sender, EventArgs e)
        {

            await Navigation.PushModalAsync(new NavigationPage(new MainPage()));            

        }

本质上,我相信应该有一种方法可以从 MainPage 中选择标签页索引或标签页。但是不确定如何正确实施。

使用上面的代码,它会将我导航回 MainPage 但是到第一个加载的选项卡。我需要它位于第 4 个选项卡或 OffersPage 上。

任何援助将不胜感激。

标签: c#xamlxamarinxamarin.formstabbedpage

解决方案


如果你想在 5 个标签页之间导航,你可以使用MessagingCenter

在标签页中

public MyTabbedPage()
{
   InitializeComponent();

   MessagingCenter.Subscribe<Object, int>(this, "click", ((arg, idx) => {

        // idx the index of pages in tabbed that you want to navigate

        CurrentPage = this.Children[idx];

   }));




}

并在点击框架时发送消息

 MessagingCenter.Send<object, int>(this,"click",2);// if you want to navigation to the third page .

推荐阅读