Xamarin Forms如何使用内容页面上的按钮切换到另一个导航页面

时间:2019-01-02 03:13:37

标签: xamarin.forms

在我的应用中,我创建了包裹在导航页面中的“内容”页面,并将这些导航页面添加到TabbedPage中。

我想要的是使用顶部的TabbedPage作为菜单浏览我的应用程序。一切正常,只是我想在某些内容页面上添加一个按钮,该按钮可以跳转到另一个内容/导航页面。

如何在TabbedPage中“选择”另一个导航页面?

编辑:添加现有代码...

所以我在MainPage.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:views="clr-namespace:JobAidTurbo.Views"
            x:Class="JobAidTurbo.Views.MainPage">
    <TabbedPage.Children>

        <NavigationPage Title="Main">
            <x:Arguments>
                <views:MainMenu />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage Title="Cheeses">
            <x:Arguments>
                <views:Cheeses />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
    </TabbedPage>

在MainPage.xaml.cs中是:

using System;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace JobAidTurbo.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : TabbedPage
    {
        public MainPage()
        {
            InitializeComponent();
            MessagingCenter.Subscribe<Object, int>(this, "click", (sender, arg) =>
            {
                CurrentPage = Children[arg];
            });
        }
    }

}

在MainMenu.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="JobAidTurbo.Views.MainMenu"
             NavigationPage.HasNavigationBar="False">
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
             </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button Text="Cheeses"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"
                BackgroundColor="Blue"
                    TextColor="White"
                    FontSize="36"
                    WidthRequest="350"
                    HeightRequest="200"
                                    Grid.Row="0"
                Grid.Column="0"
                    Clicked="Cheeses_Clicked"
                                        />
            <Button Text="Meats"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"
                BackgroundColor="Blue"
                    TextColor="White"
                    FontSize="36"
                    WidthRequest="350"
                    HeightRequest="200"
                Grid.Row="0"
                Grid.Column="1"
                    />
</Grid>
    </ContentPage.Content>
</ContentPage>

在MainMenu.xaml.cs中,我拥有:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace JobAidTurbo.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainMenu : ContentPage
    {
        public MainMenu ()
        {
            InitializeComponent ();
        }

        private void Cheeses_Clicked(object sender, EventArgs e)
        {
            MessagingCenter.Send<Object, int>(this, "click", 2);
            //2 is the num of the contentPage that you want to select


        }

    }
}

1 个答案:

答案 0 :(得分:0)

解决方案:

您可以使用MessagingCenter。请参考以下代码。

  

在clickEvent按钮中

button.Clicked += (sender, e) =>
  {
     MessagingCenter.Send<Object,int>(this,"click",2); 
     //2 is the num of the contentPage that you want to select

   };
  

在TabbedPage(构造函数)中

public MyTabbedPage()
    {
        InitializeComponent();

        //. . .

        MessagingCenter.Subscribe<Object,int>(this, "click", (sender,arg) =>
        {               
            CurrentPage = Children[arg];              
        });

现在,单击按钮时,tabbedPage将选择第三个contentPage。

有关MessagingCenter的更多详细信息,您可以参考here