Xamarin – Get values back from modal

So to return a value from a modal in Xamarin here is what I generally do …

Create a return class if you want to return a complex type like so ….

    public class ModalReturnDataClass

        public bool BoolValue { get; set; }
        public string StringValue { get; set; }

Create a listener in the calling form like so ….

            MessagingCenter.Subscribe(this, "PictureSelectionReturn", async (sender, arg) =>
                Debug.WriteLine("PictureSelectionReturn received");
                Debug.WriteLine($"Bool value {arg.BoolValue}");
                Debug.WriteLine($"String value {arg.StringValue}");

Create a sender in the modal like so ….

            var tmpObject = new ModalReturnDataClass() { BoolValue = true, StringValue = "Coming back from modal" };
            MessagingCenter.Send(Application.Current, "PictureSelectionReturn", tmpObject);

That’s really all there is to it ….

If you have a better way please let me know so I can share it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s