After closing the page execute the function

fakhravarifakhravari Member ✭✭✭
edited January 11 in Xamarin.Forms

Hi
The pop up page is open window
How do I execute another function on the previous page after closing the page?

Tagged:

Best Answers

  • fakhravarifakhravari ✭✭✭
    Accepted Answer

    protected override async void OnAppearing()
    MessagingCenter.Subscribe((App)Application.Current, "RefreshBag", (sender) => {
    RefreshBag();
    });

                MessagingCenter.Send<App>((App)Xamarin.Forms.Application.Current, "RefreshBag");
    
                Navigation.PopModalAsync();
    

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @fakhravari said:
    Hi
    The pop up page is open window
    How do I execute another function on the previous page after closing the page?

    It depends what you mean by a "pop up page".

    Depending on what you mean, you might want to consider overriding the OnAppearing() method of the previous page, or having the "pop up page" send a message using MessagingCenter that the previous page subscribes to. There are other ways too, but without more detail I'd suggest starting with one of those.

  • fakhravarifakhravari Member ✭✭✭
    edited January 11

    hi

    page 1 >> open
    Navigation.PushModalAsync(new ShopingCart());

    page 2 > close click
    Navigation.PopModalAsync();

    in call method to page 1 ??

    Give a practical example
    Thankful

  • fakhravarifakhravari Member ✭✭✭
    edited January 11

    thanks

    Could not create code

    I checked the link
    If possible make a simple example

    :)

  • JohnHardmanJohnHardman GBUniversity mod

    @fakhravari

    You wrote "Could not create code" within 2 minutes of me posting guidance and a link about what you need to do, even though the page that I linked to says it takes 4 minutes to read. Read that page, try to write the code. If you cannot get it to work after putting in some effort, post the code that you wrote to this thread.

  • fakhravarifakhravari Member ✭✭✭
    Hi
    I had read this article before

    On the words
    MessagingCenter.Send<MainPage>(this, "Hi");
    gave this error
  • fakhravarifakhravari Member ✭✭✭
    Accepted Answer

    protected override async void OnAppearing()
    MessagingCenter.Subscribe((App)Application.Current, "RefreshBag", (sender) => {
    RefreshBag();
    });

                MessagingCenter.Send<App>((App)Xamarin.Forms.Application.Current, "RefreshBag");
    
                Navigation.PopModalAsync();
    
  • JohnHardmanJohnHardman GBUniversity mod
    edited January 11

    @fakhravari

    So, as per my earlier post at https://forums.xamarin.com/discussion/comment/400276/#Comment_400276 :-)
    Yes, I could have given you the exact code, but it's better to learn by doing than by copy-and-pasting.

    BTW - don't forget to unsubscribe again in the first page, otherwise you'll likely end up with a memory leak.

  • fakhravarifakhravari Member ✭✭✭

    unsubscribe > @JohnHardman said:

    @fakhravari

    So, as per my earlier post at https://forums.xamarin.com/discussion/comment/400276/#Comment_400276 :-)
    Yes, I could have given you the exact code, but it's better to learn by doing than by copy-and-pasting.

    BTW - don't forget to unsubscribe again in the first page, otherwise you'll likely end up with a memory leak.

    Unsubscribe
    protected override void OnDisappearing()
    for page 1 or 2 ?

  • JohnHardmanJohnHardman GBUniversity mod

    @JohnHardman said:
    @fakhravari
    If using PushModalAsync and PopModalAsync, I'd suggest using MessagingCenter.
    The original page subscribes to a message before calling PushModalAsync.
    The modal page sends a message just before calling PopModalAsync.
    When the original page receives the message, it calls the method that you want to execute.

    See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

    Don't forget to mark as Accepted Answer the post that provided the answer to your question.

  • JohnHardmanJohnHardman GBUniversity mod

    @fakhravari said:
    unsubscribe > @JohnHardman said:

    @fakhravari

    So, as per my earlier post at https://forums.xamarin.com/discussion/comment/400276/#Comment_400276 :-)
    Yes, I could have given you the exact code, but it's better to learn by doing than by copy-and-pasting.

    BTW - don't forget to unsubscribe again in the first page, otherwise you'll likely end up with a memory leak.

    Unsubscribe
    protected override void OnDisappearing()
    for page 1 or 2 ?

    Keep things symmetrical. The page that does the Subscribe will normally also do the Unsubscribe.

Sign In or Register to comment.