How to navigate to a different content page after a function is called

310 views Asked by At

I have an app that has 2 tabs in Xamarin forms (iOS side). I'd like to know how to make my app navigate to a different content page after a function is called. Let me show you what I mean in code:

these are my two functions in my content page:

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

            if (file == null)
                return;
        }

After calling the TakePhotoButton_Clicked() I'd like to force my app to navigate to my other content page.

in pseudocode it would look like:

NavigateTo(MyOtherContentPage);

But im not sure how that would work or if something like that exists. Any suggestions?

1

There are 1 answers

3
Junior Jiang On

Welcome to SO!

If Root page of MainPage is NavigationPage , such as: MainPage = new NavigationPage (new Page1Xaml ()); , you can use Navigation.PushAsync to navigate to another page as follow :

async void OnNextPageButtonClicked (object sender, EventArgs e)
{
  await Navigation.PushAsync (new Page2Xaml ());
}

Else if Root page is a normal Page, you can use model naivgation method to navigate to another page as follow:

async void OnItemSelected (object sender, SelectedItemChangedEventArgs e)
{
    ...
    await Navigation.PushModalAsync (detailPage);
  }
}

More info can refer to Performing Navigation and Pushing Pages to the Modal Stack.