简体   繁体   中英

Xamarin firing OnAppearing event in wrong page after close browser

OnAppearing Called at wrong page after close browser Browser.OpenAsync

I would like to describe the scenario :

I have 2 pages
Main Home.xaml
Sub ItemDetailPage.xaml

When I Select an item from List in Home.xaml I do the following:

async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            var item = args.SelectedItem as SelectedItem;
            if (item == null)
                return;

            await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?ItemId={item.Id}");
            ((ListView)sender).SelectedItem = null;
        }

In the page ItemDetailPage.xaml I clicked GoToLink method

private async void GoToLink(object sender, EventArgs e)
        {
             await Browser.OpenAsync($"https://website.com/");
        }

The browser opened without any issue until I close it the code will call OnAppearing() related to Home.xaml page and keep ItemDetailPage.xaml page focused, Then I press back in ItemDetailPage.xaml

await Shell.Current.GoToAsync("..");

The screen back to Home.xaml page but without calling OnAppearing()

I do workaround for this problem
by using MessagingCenter

AS following:

After close ItemDetailPage.xaml the only event occur in this page was OnDisappearing()
So before opened the browser I do create a flag IsBrowserOpened (Static variable)

ItemDetailPage.xaml :

        private async void GoToLink(object sender, EventArgs e)
        {
             IsBrowserOpened= true;
             await Browser.OpenAsync($"https://website.com/");
        }
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            if (IsBrowserOpened)
            {
                MessagingCenter.Send(this, "updateFromBrowser", 1);
            }
        }

Home.xaml Page

public partial class Home : ContentPage
    {

        public Home()
        {
            InitializeComponent();
           
            MessagingCenter.Subscribe<ItemDetailPage, int>(this, "updateFromBrowser", async (obj, item) =>
            {
                // Update home page for incoming new data

            });

        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM