Nested subscription to messages on xamarin forms

100 views Asked by At

I'm new with Xamarin forms and don't know how to deal with this case. I've tryed to implement it in several ways but with no success.

I have a page where when user makes an action (write a text on a text box and send it with enter key) my app must make some checkings. Depending on the result of the checks, it could be necessary to show a modal page with a list of item to select. Ones user makes the selection process must continue with other checks. And here is my problem, because in this next checkings I have to show another page. User must make a selection/enter some date, and then continue to complete the proccess, but this page is not appear. I'm using the messagingCenter to subscribe to the modal pages. First modal page appears and makes the selection well. Second modal page is never shown and then proccess never complets. Here is some of my code:

                NavigationPage navigationPage = new NavigationPage(new ListItemsPage(products));
                Navigation.PushModalAsync(navigationPage);

                MessagingCenter.Subscribe<ListItemsPage, Guid?>(this, "Select product", (obj, item) =>
                {
                    try
                    {
                        if (item != null)
                        {
                            product = products.SingleOrDefault(x => x.Guid == item);
                            if (product != null) ProcessLine(product);
                        }                            
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        MessagingCenter.Unsubscribe<ListItemsPage, Guid?>(this, "Select product");
                    }                       
                });

On ListItemsPage I have this code whe item is selected:

private  void MenuItem_Clicked(object sender, EventArgs e)
    {
         // some logic...         
        Navigation.PopModalAsync();
        MessagingCenter.Send(this, "Select product", SelectedGuid);            

    }

SelectedGuid is a Guid type data and when debbugin is well selected. Problems comes when goes to ProcessLine method.

private void ProcessLine(Product product) {
    // make some logic...
    NavigationPage navigationPage = new NavigationPage(new ControlUnitsPage(model));
    Navigation.PushModalAsync(navigationPage);
    
    MessagingCenter.Subscribe<ControlUnitsPage, ControlUnits>(this, "Select units, date and lot code", (obj, item) =>
    {
           try
                    {
                        if (item != null)
                        {
                            _date = item.Date;
                            _code = item.Code;
                            _units = item.Units;

                            Save(productLine, product, _units, _date,_code);
                        }
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        MessagingCenter.Unsubscribe<ControlUnitsPage, ControlUnits>(this, "Select units, date and lot code");
                    }
                    
                });
   
 }

ControlUnitsPage has the same structure as the last one page. First makes a PopModalAsync and then sends the message sending an instance of ControlUnits type.

    private void Button_Clicked(object sender, EventArgs e)
    {
       //some logic...
       Item = new ControlUnits() { Date = DateField.Date, Code = CodeField.Text, Units = int.Parse(SelectedUnits.Value.ToString()) };
       Navigation.PopModalAsync();
       MessagingCenter.Send(this, "Select units, date and lot code", Item);         
    }

I think problem is in the order of invoking method but dont know what is the properly order because I am not able to understand how pushmodal, popmodal methods work, whether or not I should use await with it if after that comes a subscription. I really don't know and I need help, please.

Thank you so much!

1

There are 1 answers

6
Jason On

your Send and Subscribe calls both need to use matching parameters

if Subscribe looks like this

MessagingCenter.Subscribe<ControlUnitsPage, ControlUnits>(this, "Select units, date and lot code", (obj, item) => ... );

then Send needs to match

MessagingCenter.Send<ControlUnitsPage, ControlUnits>(this, "Select units, date and lot code", Item);