question

CraigMuckleston avatar image
0 Votes"
CraigMuckleston asked JarvanZhang-MSFT answered

CrossMedia causing AppShell page callback

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.