【问题标题】:Xamarin Forms How do I use a button on a content page to switch to another Navigation PageXamarin Forms 如何使用内容页面上的按钮切换到另一个导航页面
【发布时间】:2019-01-10 12:48:56
【问题描述】:

在我的应用中,我创建了包含在导航页面中的内容页面,并将这些导航页面添加到 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


        }

    }
}

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    解决方案:

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

    在按钮点击事件中

    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

    【讨论】:

    • 它没有用。请记住,我对 Xamarin Forms 非常陌生。我将在上面更新我的代码
    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 2016-10-08
    • 2010-10-08
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多