Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to programatically navigate to a previous tab within a tabbed page

ikreieikreie Member ✭✭

I'm making a forms application on the ios side and am having issues with navigation. I have 2 tabs in my app, "about" and "camera". I want to be able to click the camera tab, open the camera to take a picture, and after i'm finished with taking the picture, the control goes back to the "about" tab. I've uploaded a gif for a more visual example, but pretend at the end that after the image is cropped the control goes back to the "about" tab automatically.

How can i accomplish this?

I've tried using Navigation.PushAsync(new UploadPage()); but it just creates new instances of my upload page. I simply want to return back to the previous about tab.

here's the rest of my code for more context:

mainPage.XAML

 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:EmployeeSnapshot;assembly=EmployeeSnapshot" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="EmployeeSnapshot.MainPage" CurrentPageChanged="OnCurrentPageChanged">
     <local:UploadPage Title="Upload" IconImageSource="upload.png"/>
     <NavigationPage Title="Camera" IconImageSource="camera2.png">
         <x:Arguments>
             <local:CameraPage />
         </x:Arguments>
     </NavigationPage>
</TabbedPage> 

CameraPage.xaml.cs

namespace EmployeeSnapshot
{
    public partial class CameraPage : ContentPage
    {
        public CameraPage()
        {
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            TakePhotoButton_Clicked();

        }

        async void TakePhotoButton_Clicked()
        {

            if (App.pictureTaken)
            {
                return;
            } 

            App.pictureTaken = true;

            //Allows users to take pictures in the app
            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                DisplayAlert("No Camera", "Camera is not available.", "OK");
                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                //Sets the properties of the photo file 
                SaveToAlbum = true,
                PhotoSize = PhotoSize.MaxWidthHeight,
                DefaultCamera = CameraDevice.Rear
            });

            Navigation.PushAsync(new UploadPage());

            if (file == null)
                return;
        }
    }
}

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 5

    How to programatically navigate to a previous tab within a tabbed page

    To navigate to a tab, try to use the TabbedPage.CurrentPage property. Add a global static instance of the TabbedPage and get the instance in the child page to perform navigation.

    Check the code:

    App.xaml.cs

    public partial class App : Application
    {
        public static TabbedPage1 tabbedPage = new TabbedPage1();
        public App()
        {
            InitializeComponent();
    
            MainPage = tabbedPage;
        }
    }
    

    Page2.xaml.cs

    private async void method()
    {
        ...
        App.tabbedPage.CurrentPage = App.tabbedPage.Children[0];
    }
    

    If you don't want to navigate to a specified index tab, you could add a property in the App class to store the previous tab index.

    public partial class App : Application
    {
        public static int previous_index;
        ...
    }
    

    Set value to the index property in the OnAppearing method of a child page.

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Page page = App.tabbedPage.CurrentPage;
        App.previous_index = TabbedPage.GetIndex(page);
    }
    

  • JarvanJarvan Member, Xamarin Team Xamurai

    @ikreie Hi, any updates? If the solution helps you to solve the issue, please accept it as the answer. It'll help others who face the similar problem. If you are facing some issues while implementing, try to post the particular error with the corresponding codes here.

  • ikreieikreie Member ✭✭

    @YelinZh Yes this answered my question but i am not allowed to select your answer as correct!

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 12

    @ikreie said:
    @YelinZh Yes this answered my question but i am not allowed to select your answer as correct!

    You could click the 'Yes' button to accept the solution as the answer.

Sign In or Register to comment.