Forum Xamarin.Forms

Using Rg.Plugins.Popup, PopUp ListView Page, How to return the selected item back to the page?

DavidFreireDavidFreire USMember ✭✭

I'm Using Rg.Plugins.Popup, and "Popping" a ListView Page with a bunch of clients in the listview and i want to return a cliente selected back to the page that a was before, I try something like this...

            ClientsListView.ItemTapped += async (sender, args) =>
            {
                ClientsListView.SelectedItem = null;
                var item = args.Item as Clientes;
                if (item == null) return;
                await Navigation.PopPopupAsync(item);
            };

But obviously this didn't work...

Some help would be great...

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Don't worry about sending the event back.
    The popup still exists in memory after its closed, as long as your caller keeps it in scope.
    So try this logic...

    SomeMethod()
    {
       `await` the opening of the popup..
       The popup then has a selected object property.
       The popup closes
       The caller then checks the selected object property of the popup that it pushed.
    }
    

    No different that when you have an OpenFileDialog from Windows. You open the dialog. The user selects a file. Closes the dialog. Then you check the FilePath property of the dialog.

    Same pattern here.

  • DavidFreireDavidFreire USMember ✭✭

    @ClintStLaurent said:
    Don't worry about sending the event back.
    The popup still exists in memory after its closed, as long as your caller keeps it in scope.
    So try this logic...

    SomeMethod()
    {
       `await` the opening of the popup..
       The popup then has a selected object property.
       The popup closes
       The caller then checks the selected object property of the popup that it pushed.
    }
    

    No different that when you have an OpenFileDialog from Windows. You open the dialog. The user selects a file. Closes the dialog. Then you check the FilePath property of the dialog.

    Same pattern here.

    Can you send a example, i really never do it...

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm not writing your code for you. Put in some time, energy, thought, trial & error at it then post your code and any errors you get. I'm happy to help you - To become a better coder and that is only done through practice and working to stretch your skillset forward. Skill and learning aren't accomplished through copy/paste.

  • DavidFreireDavidFreire USMember ✭✭
    edited May 2017

    @AlessandroCaliaro said:
    I think you can pass the "father" ViewModel to the "Popup", update it and when the Popup is closed, you should have your "father" ViewModel updated.

    Otherwise, MessaggingCenter is a good solution

    I try this but the ViewModel didn't update the previous page...

    Can i send a ListView Selected item through messagingcenter??
    What page would be the Subscribe, the Main Page or the Popup Page?

  • NMackayNMackay GBInsider, University admin

    If your using Prism you can use this pluggin which allows you to hook popups into the navigation service and then you can just pass back parameters like you normally would.

    https://github.com/dansiegel/Prism.Plugin.Popups

     public async void SelectFav(AppFavourite fav)
            {
                try
                {
                    Busy = true;
                    CanNavigate = false;
    
                    await _navigationService.PopupGoBackAsync(new NavigationParameters { { NavigationParams.NavContextPopupFav, fav } }, false);
    
  • DavidFreireDavidFreire USMember ✭✭

    @AlessandroCaliaro said:
    I think you can pass the "father" ViewModel to the "Popup", update it and when the Popup is closed, you should have your "father" ViewModel updated.

    Otherwise, MessaggingCenter is a good solution

    Sorry for taking so long to respond, i had a job that needed full Atention...
    I end up doing this job throught MessagingCenter Sending the SelectedItem of the ListView on the PopUp back to the page i was early and it work's perfectily, Many Thanks...

  • @NMackay

    which navigation method do i need to override on my main page so that i can work with the navigation parameters returned from the popup page using

    await _navigationService.PopupGoBackAsync ...........

    public override void OnNavigatedTo(NavigationParameters parameters)
    {
        SubElement = parameters.GetValue<string>("subelement");            
    }
    

    doesn't seem to be firing when i use this approach

    thanks in advance,

    jas

  • NMackayNMackay GBInsider, University admin

    It should work fine using version 1.1.0 of Prism.Plugin.Popup, I'm using Unity, Prism 6.3 & Forms 2.3.5-pre6 at present.

    In the popup viewmodel:

        var navParams = new NavigationParameters();
            navParams.Add(NavigationParams.NewCustomer, item);
        await base.Navigation.PopupGoBackAsync(navParams);
    

    And in the viewmodel of the original calling page

    public override async void OnNavigatedTo(NavigationParameters parameters)
            {
    
                // Anything to do ?
                if (parameters.Count == 0) return;
    
                if (parameters.ContainsKey(NavigationParams.NewCustomer))
                {
            // Do something
            }
    
  • my popup page has

            private async void OnSubElementSelectedCommandExecuted(SurveyDesignStockCondition subElement)
            {
                var navigationParams = new NavigationParameters
                {
                    { "subelement", subElement.SubElement },
                    { "subelementid", subElement.SubElementId }
                };
    
                await NavigationService.PopupGoBackAsync(navigationParams);
            }
    

    while my calling page that does not fire is

                public override void OnNavigatedFrom(NavigationParameters parameters) 
                {
                    SubElement = parameters.GetValue<string>("subelement");            
                }
    

    ill try on a clean app this evening and see how i get on

  • NMackayNMackay GBInsider, University admin

    I'd have to peek about in the source and ask Dan but OnNavigatedFrom may not fire,we don't use that approach, we pass the parameters when we push the Popup and use OnNavigatedTo when passing back in the original calling VM, for example, OnNavigatingTo won't fire. I'd stick to using OnNavigatedTo for Popups & Prism.

  • sorry, OnNavigatedTo , i was playing around to see which would fire.

  • NMackayNMackay GBInsider, University admin

    If you don't get it working, knock up a little repo project and I'll take a look tomorrow.

  • @NMackay

    thanks for the offer.

    ive made a copy of my app and stripped it down and OnNavigatedTo does indeed fire so ill spend some time over the weekend looking over the full app again.

    thanks again

  • UdaraAlwisUdaraAlwis LKMember ✭✭✭

    Here's the solution you're looking for ;)

    https://theconfuzedsourcecode.wordpress.com/2017/07/26/an-await-able-transparent-custom-popup-input-dialog-in-xamarin-forms/

    You could achieve this requirement even without the MVVM support, as shown in the article.

    Here's the exact ListView input selection implementation you're looking for:
    https://github.com/UdaraAlwis/Xamarin-Playground/blob/master/XFCustomInputAlertDialog/XFCustomInputAlertDialog/XFCustomInputAlertDialog/InputViews/SelectableInputView.xaml.cs

    Cheers! :)

  • nick5454nick5454 USUniversity ✭✭✭

    It's in the OnNavigatedTo event

    ` if (parameters.TryGetValue("SelectedItem", out MySelectedObjectType selectedItem))
    {

                return;
            }`
    

    That keeps it in the MVVM pattern.

  • SerenaSerena Member ✭✭

    @DavidFreire said:

    @AlessandroCaliaro said:
    I think you can pass the "father" ViewModel to the "Popup", update it and when the Popup is closed, you should have your "father" ViewModel updated.

    Otherwise, MessaggingCenter is a good solution

    Sorry for taking so long to respond, i had a job that needed full Atention...
    I end up doing this job throught MessagingCenter Sending the SelectedItem of the ListView on the PopUp back to the page i was early and it work's perfectily, Many Thanks...

    Hello Could you share a sample code of your final solution of sending the SelectedItem back? I am facing the same issue and just cant image how to do it. Thank you.

Sign In or Register to comment.