How to Transfer Data from One Page to Another in .NET MAUI
A list page that constructs the detail page with an employee in the constructor works until the next navigation needs the same object. Himanshu Tyagi pulls that hand-off into a service so the pages do not know how each other is built. The same idea applies to Xamarin.Forms.
What you’ll learn
- Why the constructor gets awkward — the list page has to know the detail page’s constructor, and the data is stuck on that one navigation
ISharedService—Add<T>(key, value)andGetValue<T>(key), implemented as a dictionary onSharedService- Registration — view models as transient, the shared service as a singleton in
MauiProgram, so both pages see the same dictionary - The hand-off — the home view model stores
"SelectedEmployee"and pushesDetailPage. The detail view model reads that key in its constructor - The UI thread — the push is wrapped in
MainThread.BeginInvokeOnMainThread
Read the post for the folder layout and the sample repository. The dictionary is the whole mechanism, so the interesting part is where you put it.