XAML 在选项卡式页内的选项卡内导航

6g8kf2rb  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(233)

inside one tab i use this code and add button to navigate to another content page我在Xamarin Forms中做了一个选项卡式页面,在选项卡式页面中我为选项卡取了4个内容页面。在这些内容页面中,有一些按钮,当我在按钮上添加单击事件时。在另一个内容页面中导航。按钮不触发。请为这个问题提供一些解决方案。

<NavigationPage Title="Home" IconImageSource="HomeGrey.png">
    <x:Arguments >
        <local:NewHomeScreen Title="Home" ></local:NewHomeScreen>
    </x:Arguments>
</NavigationPage>

<NavigationPage Title="Pickup" IconImageSource="PickupGrey.png">
    <x:Arguments>
        <local:PickupPage ></local:PickupPage>
    </x:Arguments>
</NavigationPage>

<NavigationPage Title="Setting" IconImageSource="settings.png">
    <x:Arguments>
        <local:NewSetting ></local:NewSetting>
    </x:Arguments>
</NavigationPage>
<NavigationPage Title="Support" IconImageSource="SupportGrey.png">
    <x:Arguments>
        <local:SupportPage ></local:SupportPage>
    </x:Arguments>
</NavigationPage>
bvjxkvbb

bvjxkvbb1#

如果你想在点击按钮时切换到TabbedPage的下一个标签页,你可以尝试使用EventHandler来实现这一点。
我实现了这个功能,可以参考下面的代码:

首页. xaml.cs      

public partial class FirstPage : ContentPage 
      {
         public event EventHandler<string> ButtonClickedHandler;

       public FirstPage ()
            {
                  InitializeComponent ();
            }

            private   void Button_Clicked(object sender, System.EventArgs e)
            {
              ButtonClickedHandler?.Invoke(this, "topage2");
            }
      }

首页.xaml   

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   x:Class="TabbedPageWithNavigationPage.FirstPage"
                   IconImageSource="test.png"
                   Title="Today">
      <ContentPage.Content>
            <StackLayout>
                  <Label Text="Today's appointments go here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />

            <Button Text="navigate" Clicked="Button_Clicked"/>
        </StackLayout>
      </ContentPage.Content>
</ContentPage>

主页面.xaml.cs

public partial class MainPage : TabbedPage 
      {
            public MainPage ()
            {
                  InitializeComponent ();

            tabPage1.ButtonClickedHandler += delegate (object obj,string str) {
                CurrentPage = tabPage2;
            }; 

        }
      }

主页面.xaml

<?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:TabbedPageWithNavigationPage;assembly=TabbedPageWithNavigationPage" 
            x:Class="TabbedPageWithNavigationPage.MainPage"
            x:Name="tabbedPage"
            >
      <local:FirstPage   x:Name="tabPage1" />
      <NavigationPage Title="Schedule" IconImageSource="schedule.png"   x:Name="tabPage2">
            <x:Arguments>
                  <local:SecondPage />
            </x:Arguments>
      </NavigationPage>
    <local:ThirdPage x:Name="tabPage3"/>
</TabbedPage>

更新日期:

我用你的代码做了一个测试,但是它抛出了错误**System.InvalidCastException:** 'Specified cast is not valid.'。因为当前页面是ContentPage,所以你不能将它转换为TabbedPage
如果要导航到另一个页面而不是其他选项卡,可以尝试编写代码:

Navigation.PushAsync (new DeliveryDetailPaidPage());

相关问题