Pass data from ModalPage back to parent

onedevteam.comonedevteam.com USMember ✭✭
edited May 2017 in Xamarin.Forms

I'm using this in my home page to show list of items in a modal child.

       await Navigation.PushModalAsync(new ModalChildPage());

On child page, i use ListView to show data, and when user tap item, i would like to return selected item id back to home page.

I'm still new in Xamarin.Forms, and can't figure how to do this.
I do have ItemSelected event and i have selected item in it, but don't know how to pass that data back with Navigation.PopModalAsync or something else.

Can someone help? Thanks

Best Answer

  • MagnusWallonMagnusWallon US ✭✭✭
    edited May 2017 Accepted Answer

    There are different ways to do this, the way I do it and I know it's not standard but I think it works, is to add a reference to the main page when I call the modal page.

    ** Ex:**

    await Navigation.PushModalAsync(new ModalChildPage(this));
    
    **and on ModalPage:**
    
    public partial class ModalPage
    {
            MainPage mp;
    
            public ModalPage(MainPage m)
            {
                mp=m;
            }
            public popFunc()
            {
                // now you can call any function on the main page just before you pop
                mp.myFunc();
                Navigation.PopModalAsync();
            }
    }
    

Answers

  • MagnusWallonMagnusWallon USMember ✭✭✭
    edited May 2017 Accepted Answer

    There are different ways to do this, the way I do it and I know it's not standard but I think it works, is to add a reference to the main page when I call the modal page.

    ** Ex:**

    await Navigation.PushModalAsync(new ModalChildPage(this));
    
    **and on ModalPage:**
    
    public partial class ModalPage
    {
            MainPage mp;
    
            public ModalPage(MainPage m)
            {
                mp=m;
            }
            public popFunc()
            {
                // now you can call any function on the main page just before you pop
                mp.myFunc();
                Navigation.PopModalAsync();
            }
    }
    
  • wilconwilcon USMember ✭✭✭

    When I started with Xamarin.Forms a few months ago I had had the same problem but this forum post and guide really helped me out understanding the basics.

    https://forums.xamarin.com/discussion/50464/popasync-pass-result
    http://blog.adamkemp.com/2015/03/decoupling-views.html

  • wilconwilcon USMember ✭✭✭

    @JohnMiller said:
    @onedevteam.com,

    I'll offer some quick advice on ways you can accomplish this that I have seen in the wild.

    1. A simple approach is to use the MessagingCenter. Create a message that you send when you call PopModalAsync and send along some payload of data. Subscribe to this message in your parent class so that you can act on it. More info on that is available in the MessagingCenter docs.
    2. Use some way to persist data (SQL, ApplicationProperties, etc) and serialize some payload when you PopModalAsync. Then in the OnAppearing of your parent page, you can read this data from storage. You'll have to distinguish if OnAppearing was called in the right context though.
    3. A lot of MVVM frameworks help solve this by providing an abstraction around Navigation. That might be too much for your application, but it's worth mentioning. They provide ways to pass a payload with the navigation methods. Things like "Prism for Xamarin.Forms" are a great example of providing this in it's NavigationService.

    I hope that helps and look forward to what else the community could recommend.

    3 is the way to go in most situations, good call.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you can pass the FatherPage's viewmodel to ChildPage and modify it

  • NMackayNMackay GBInsider, University mod

    +1 for Prism, it makes this really easy, you can even use a plugin to wire Rg.Plugin.Popups into the navigation service and pass parameters back from modals and popups.

    There's lots of other benefits to using Prism but this feature is really useful.

  • onedevteam.comonedevteam.com USMember ✭✭

    Thanks for answers. I'm still learning Xamarin.Forms, so i would like to avoid any external things, like prism. I'll go with @MagnusWallon answer for now..

Sign In or Register to comment.