c# - 如何从 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 上。
任何援助将不胜感激。
解决方案
如果你想在 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 .
推荐阅读
- node.js - ValidationError:产品验证失败:年龄:需要路径“年龄”。,名字:需要路径“名字”
- numpy - 获取颜色和颜色列表之间的欧几里得距离
- python - 如何在 Python 中每 X 次迭代执行一次操作,直到无穷大?
- pgadmin - pgAdmin 查看索引列表
- javascript - 为什么有时使用点符号访问对象属性会返回未定义?
- node.js - Passport/Express:在注册时创建新用户后无法验证用户
- python - 即使密码正确,打开 Zip 文件(受密码保护)也会出现错误“密码错误”
- python - 实现函数 avg(),它将包含数字列表的列表作为输入
- java - “包 jdk.jshell.spi 不存在” - 仅在一台 PC 上出错
- java - 这是否正确