locked
PopAsync pass result RRS feed

  • Question

  • User66293 posted

    Hi,

    I use navigation page to navigate from page A to page B. Page B only contains a listview with values. When a users clicks an item in the listview. The page is poped. Now when popping page B, I would like for page A to receive the value selected in Page B.

    In short: 1) NavigationPage 2) Navigate from page A to page B 3) Select value in Page B 4) return selected value from page B to page A

    Preferably I would like to avoid a static variable!

    Thanks in advance!

    Friday, September 11, 2015 9:49 AM

Answers

  • User181 posted

    MessagingCenter is not appropriate for this kind of use case because it is a broadcast API with no control over which object actually should be receiving the message. That is only useful if all of your pages are singletons, which probably isn't the case. I wish people would stop treating MessagingCenter as a panacea.

    And Fredy, just because modal dialogs in old-school Windows programming had synchronous APIs doesn't mean that's the definition of "modal". :)

    IMO the "right way" to get results back from a page is described here. The tl;dr is "use events to send back results, and do the pop from the originating page, not from the new page".

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, September 11, 2015 5:05 PM

All replies

  • User90084 posted

    @NicolasHotterbeekx http://developer.xamarin.com/guides/cross-platform/xamarin-forms/messaging-center/

    Friday, September 11, 2015 9:58 AM
  • User66293 posted

    Hmm, completely forgot about the messaging center! Mainly because I don't like it! So is this really the only option?

    Friday, September 11, 2015 10:11 AM
  • User90084 posted

    @NicolasHotterbeekx I'm pretty sure I've seen examples of other ways of doing this, but when constrained for time, MC works well enough. Main thing about using MC, imo, is making sure to Subscribe/Unsubscribe properly. If not constrained for time, might be worth the time investment to look for the other options.

    Friday, September 11, 2015 10:15 AM
  • User65389 posted

    @Tekton and @NicolasHotterbeekx:

    Main thing about using MC, imo, is making sure to Subscribe/Unsubscribe properly

    That's exactly the problem.
    Unfortunately, a "modal" page in XF is not really "modal" (in my understanding of a "modal" page, the code stops, until the modal page is released, what is not the case). I have a similar use case in my app: - the user taps on a search-icon on a page (A) - I show a "modal" search-page (B) - the user select some search items on the search page (B) and press a search Button => Then the search-page (B) has to be closed (PopModalAsync), the data have to be queried from web-service and the calling page (A) (with the search-icon) has to be updated
    If the code on page (A) would stop (as I first have expected), this would be no problem... I then have searched for more than a week, how to solve this "trivial" problem... ... and than have ended by use the MC in my app, as I don't have found another proper way for my needs.
    But I had to implement additional logic, as the behavior is not logic and also the event-firing in XF is strange.
    If the Subscribe/Unsubscribe don't work in any case, the result in my implementation is, that the MC-event (in my case: query the data) is called multiple times and the user than sees multiple items in the result . That I have to use it in a MD navigation (where the detail-page always is loaded) and that the debugger don't really works with async-events, don't make it easier...

    So... If possible, I would make it on another way (unfortunately I don't have found a proper way to solve the "trivial" problem for my use case in the time a had to search for :disappointed: )

    Friday, September 11, 2015 10:47 AM
  • User62190 posted

    I use MC for things like that. For subscribing and unsubscribing and cleaning up pages I do this.

    Friday, September 11, 2015 11:53 AM
  • User181 posted

    MessagingCenter is not appropriate for this kind of use case because it is a broadcast API with no control over which object actually should be receiving the message. That is only useful if all of your pages are singletons, which probably isn't the case. I wish people would stop treating MessagingCenter as a panacea.

    And Fredy, just because modal dialogs in old-school Windows programming had synchronous APIs doesn't mean that's the definition of "modal". :)

    IMO the "right way" to get results back from a page is described here. The tl;dr is "use events to send back results, and do the pop from the originating page, not from the new page".

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, September 11, 2015 5:05 PM
  • User90084 posted

    Well I think some would stop using it as a panacea if better was known... Nevertheless, if it inspires ire enough to be guided down the correct path, then I say, "Blood alone moves the wheels of history!" 8^)

    Friday, September 11, 2015 8:18 PM
  • User90084 posted

    @adamkemp Excellent post, thank you. :star:

    Friday, September 11, 2015 8:22 PM
  • User181 posted

    There definitely is a better alternative, and it's built in to the language: C# events.

    Friday, September 11, 2015 8:49 PM
  • User90084 posted

    @adamkemp Oddly enough, I've been guided by your decoupling views post, in the not so distant past. This time, will have to try harder to remember the basic idea of "MessagingCenter for broadcasts, Events for events". @NicolasHotterbeekx @FredyWenger @TorbenKruse Adam's blog post he's linking to here is a much better idea than MessagingCenter. In fact, right before looking at your reply, Adam, I got snagged by the very issue using MessagingCenter presents, in my own code. To quote Adam:

    In Xamarin.Forms you could also use MessageCenter to broadcast messages, but in more complex examples you may end up with multiple instances of the same kind of page in existence, and subscribers would then have to be sure to handle only the messages from the page they're actually interested in. Not only is that error prone, but in order to do that discrimination the subscriber has to have a direct reference to the sender in the first place, and then you may as well just be using events. Events are just easier for this use case. Use MessageCenter for cases where it really is a broadcast to the whole system.

    Friday, September 11, 2015 9:16 PM
  • User66293 posted

    Allright! Nice! Thanks for all the help!

    Monday, September 14, 2015 6:50 AM