Callback to return value during App Shell navigation page closing #18972
Unanswered
TrueMoniker
asked this question in
Ideas
Replies: 1 comment 4 replies
-
Hi, Thank you for the solution this is what exactly I have been searching. However I got some issue with the usage. I have created two pages and viewmodels:
NewPage1 is the parent page Inside When I debug I see the inside the Am I missing something? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Example repo
I would like a consistent way to return a value to a previous page when a page is closed. For this discussion I'll call the original page the parent page and the page that was opened and is closing the child page. For me, it's a very common use case of: being in a parent page, opening a new child page to get a specific value, then needing that value returned to the parent page when the child page is closed. I would like to see an explicit interface for returning values added to the framework.
My proposed solution:
How it works:
It's passing the return value from the child to the parent through the WeakReferenceMessenger. It handles the setup for the messenger for me, I only have to remember to open the child page using the
GoToAndReturn
method, and close the child page using theGoBackAsync
method.To use, inherit the class
ViewModelWithReturn
in your viewmodel. Set the TReturn generic parameter to the type of the object returned.Then, when you want the page to close, call
GoBackAsync
with the return object.Override Back Button
If you want to return something when the back button is pressed, you can override the back button behavior in the page xaml to call a command you define. That command should call the
GoBackAsync
method.Alternatively,
ViewModelWithReturn
provides the GoBackCommand to close the page and return the command parameter.Usage
When navigating to the page that should return a parameter, call the static methdo on the viewmodel
GoToAndReturn
.For the parameters:
this
Pros:
Cons:
Why I do it this way:
Maui doesn't have an explicit way to return a value when a page is closed. When I search around, I find two options recommended.
Option 1: Use the messaging center
based on this discussion
Use the messaging center ,now changed to
WeakReferenceMessenger
andStrongReferenceMessenger
, to pass the return value back to parent page in a message. The parent page has to subscribe to the message the child is sending. This is the method I went with, though with my own layer on top. The messenger has a lot of boiler plate code, it is not type safe, it is easy to accidentally call an orphaned subscriber that hasn't been garbage collected yet, and can be difficult to understand why the subscription is there for anyone reading the code for the first time. My implementation uses the messenger option, but attempts to resolve these issues.Option 2: Pass the value as a parameter from the child page back to the parent page
based on this discussion
I did not use this option. I have qualms with the way Maui handles parameters, discussed here. But besides that, this method of returning a value requires the parent page to handle the returned value as a parameter. If the parent page already accepts parameters, this adds another step for checking and casting the parameter that will only be there sometimes. If the parent page uses several child pages in it's lifetime, the problem is worse as each one will have a parameter condition in the parent page.
Beta Was this translation helpful? Give feedback.
All reactions