簡體   English   中英

在選項卡內導航 在選項卡式頁面內

[英]Navigation Within a Tab Inside a tabbed 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>

如果你想在單擊按鈕時切換到TabbedPage的下一個選項卡,你可以嘗試使用EventHandler來實現這一點。

我實現了這個function,你可以參考下面的代碼:

FirstPage.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>

MainPage.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()); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM