CrossMedia causing AppShell page callback

Craig Muckleston 161 Reputation points
2021-04-30T10:51:32.137+00:00

I have a main page that loads some data On Appearing.

I have another viewmodel that allows a user to select an image from their gallery. I've noticed that when CrossMedia.Current.PickPhotoAsync(...) is called, the OnAppearing function is called on the main page.

I'm looking for a way around this, so that OnAppearing is not fired when CrossMedia.Current.PickPhotoAsync(...) is called.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,825 questions
No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,851 Reputation points
    2021-04-30T11:44:10.493+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I've noticed that when CrossMedia.Current.PickPhotoAsync(...) is called, the OnAppearing function is called on the main page.

    The OnAppearing method will be called when the page becomes visible, picking a file will level the current page. That is why OnAppearing method will be called after selecting an image. We cannot prevent the OnAppearing method being called. Instead, it's available to skip the function code in the OnAppearing method.

    Try to create a status property in the page class and detect the value to skip the function code. Check the code:

       public partial class TestPage : ContentPage  
       {  
           bool theValue;  
           public TestPage()  
           {  
               InitializeComponent();  
               ...  
           }  
         
           private async void Button_Clicked(object sender, EventArgs e)  
           {     
               //pick the file  
               theValue = true;  
           }  
         
           protected override void OnAppearing()  
           {  
               base.OnAppearing();  
         
               if (theValue)  
                   return;  
         
               DisplayAlert("title", "message", "cancel");  
               theValue = false;  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

0 additional answers

Sort by: Most helpful