MicrosoftDocs / CommunityToolkit

The Official Docs for the Community Toolkits
https://learn.microsoft.com/dotnet/communitytoolkit
Creative Commons Attribution 4.0 International
102 stars 82 forks source link

MVVM Community Toolkit - Messaging in Maui #377

Open jwy2020 opened 4 months ago

jwy2020 commented 4 months ago

Type of issue

Other (describe below)

Description

I am using the messenger from the toolkit. Specifically, I am sending a message to a page from the main page. If I do not navigate to the second form before sending, the message does not get delivered to the second form. If I do navigate there and back, the messages work as expected. This seems to make sense that the registration to the message has not occurred yet.

Is there any way to send the message and then navigate to the form?

Page URL

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/messenger

Content source URL

https://github.com/MicrosoftDocs/CommunityToolkit/blob/main/docs/mvvm/Messenger.md

Document Version Independent Id

6f38cc35-64a9-f509-76b6-03469b43cd8b

Article author

@Sergio0694

Metadata

bijington commented 4 months ago

You can't send a message to something that doesn't exist yet. If you haven't navigated to your page then there won't be one in memory to receive your message. If you need to pass data between pages I would advocate for using something like this: https://learn.microsoft.com/dotnet/maui/fundamentals/shell/navigation?view=net-maui-8.0#pass-data

As an additional point, how are you registering your pages/view models? Transient/singleton/scoped? As you may be getting unexpected behavior if your page receives the message after you navigate away.