Xamarin Forms How do I use a button on a content page to switch to another Navigation Page












0















In My app, I've created Content pages wrapped in navigation pages and added those navigation pages to a TabbedPage.



What I want is to use the TabbedPage at the top as sort of a menu to navigate around my app. This sems to all be working fine, except I want to add a button on some content pages that can jump around to another one of the content/navigation pages.



How can I "Select" another of the navigation pages within the TabbedPage?



Edit: Adding existing code...



So I have in 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>


In MainPage.xaml.cs is:



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];
});
}
}

}


In MainMenu.xaml I have:



<?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>


And in MainMenu.xaml.cs I Have:



    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


}

}
}









share|improve this question





























    0















    In My app, I've created Content pages wrapped in navigation pages and added those navigation pages to a TabbedPage.



    What I want is to use the TabbedPage at the top as sort of a menu to navigate around my app. This sems to all be working fine, except I want to add a button on some content pages that can jump around to another one of the content/navigation pages.



    How can I "Select" another of the navigation pages within the TabbedPage?



    Edit: Adding existing code...



    So I have in 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>


    In MainPage.xaml.cs is:



    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];
    });
    }
    }

    }


    In MainMenu.xaml I have:



    <?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>


    And in MainMenu.xaml.cs I Have:



        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


    }

    }
    }









    share|improve this question



























      0












      0








      0








      In My app, I've created Content pages wrapped in navigation pages and added those navigation pages to a TabbedPage.



      What I want is to use the TabbedPage at the top as sort of a menu to navigate around my app. This sems to all be working fine, except I want to add a button on some content pages that can jump around to another one of the content/navigation pages.



      How can I "Select" another of the navigation pages within the TabbedPage?



      Edit: Adding existing code...



      So I have in 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>


      In MainPage.xaml.cs is:



      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];
      });
      }
      }

      }


      In MainMenu.xaml I have:



      <?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>


      And in MainMenu.xaml.cs I Have:



          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


      }

      }
      }









      share|improve this question
















      In My app, I've created Content pages wrapped in navigation pages and added those navigation pages to a TabbedPage.



      What I want is to use the TabbedPage at the top as sort of a menu to navigate around my app. This sems to all be working fine, except I want to add a button on some content pages that can jump around to another one of the content/navigation pages.



      How can I "Select" another of the navigation pages within the TabbedPage?



      Edit: Adding existing code...



      So I have in 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>


      In MainPage.xaml.cs is:



      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];
      });
      }
      }

      }


      In MainMenu.xaml I have:



      <?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>


      And in MainMenu.xaml.cs I Have:



          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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 10 at 4:48







      Graham Ballard

















      asked Jan 2 at 3:13









      Graham BallardGraham Ballard

      62




      62
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Solution:



          You can use MessagingCenter .Refer to the following code.




          in the button clickEvent




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

          };



          in the TabbedPage(constructor)




          public MyTabbedPage()
          {
          InitializeComponent();

          //. . .

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


          Now the tabbedPage will select the third contentPage whenyou click the button.



          For more detail about MessagingCenter you can refer here.






          share|improve this answer
























          • It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

            – Graham Ballard
            Jan 3 at 23:50













          • Ok ,you can update your question

            – Lucas Zhang - MSFT
            Jan 4 at 0:36











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000824%2fxamarin-forms-how-do-i-use-a-button-on-a-content-page-to-switch-to-another-navig%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Solution:



          You can use MessagingCenter .Refer to the following code.




          in the button clickEvent




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

          };



          in the TabbedPage(constructor)




          public MyTabbedPage()
          {
          InitializeComponent();

          //. . .

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


          Now the tabbedPage will select the third contentPage whenyou click the button.



          For more detail about MessagingCenter you can refer here.






          share|improve this answer
























          • It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

            – Graham Ballard
            Jan 3 at 23:50













          • Ok ,you can update your question

            – Lucas Zhang - MSFT
            Jan 4 at 0:36
















          0














          Solution:



          You can use MessagingCenter .Refer to the following code.




          in the button clickEvent




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

          };



          in the TabbedPage(constructor)




          public MyTabbedPage()
          {
          InitializeComponent();

          //. . .

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


          Now the tabbedPage will select the third contentPage whenyou click the button.



          For more detail about MessagingCenter you can refer here.






          share|improve this answer
























          • It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

            – Graham Ballard
            Jan 3 at 23:50













          • Ok ,you can update your question

            – Lucas Zhang - MSFT
            Jan 4 at 0:36














          0












          0








          0







          Solution:



          You can use MessagingCenter .Refer to the following code.




          in the button clickEvent




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

          };



          in the TabbedPage(constructor)




          public MyTabbedPage()
          {
          InitializeComponent();

          //. . .

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


          Now the tabbedPage will select the third contentPage whenyou click the button.



          For more detail about MessagingCenter you can refer here.






          share|improve this answer













          Solution:



          You can use MessagingCenter .Refer to the following code.




          in the button clickEvent




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

          };



          in the TabbedPage(constructor)




          public MyTabbedPage()
          {
          InitializeComponent();

          //. . .

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


          Now the tabbedPage will select the third contentPage whenyou click the button.



          For more detail about MessagingCenter you can refer here.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 6:11









          Lucas Zhang - MSFTLucas Zhang - MSFT

          2,6122210




          2,6122210













          • It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

            – Graham Ballard
            Jan 3 at 23:50













          • Ok ,you can update your question

            – Lucas Zhang - MSFT
            Jan 4 at 0:36



















          • It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

            – Graham Ballard
            Jan 3 at 23:50













          • Ok ,you can update your question

            – Lucas Zhang - MSFT
            Jan 4 at 0:36

















          It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

          – Graham Ballard
          Jan 3 at 23:50







          It did not work. Please bear in mind I'm very new to Xamarin Forms. I'll update above with my code

          – Graham Ballard
          Jan 3 at 23:50















          Ok ,you can update your question

          – Lucas Zhang - MSFT
          Jan 4 at 0:36





          Ok ,you can update your question

          – Lucas Zhang - MSFT
          Jan 4 at 0:36




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000824%2fxamarin-forms-how-do-i-use-a-button-on-a-content-page-to-switch-to-another-navig%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory