Xamarin Forms MessagingCenter Subscribe called two times

I'm clicking on a product item in listview in product page viewmodel to show a popup(using rg.plugin popup) for selecting one of the product variants.After selecting variant,i am sending the selected variant to product page using messagingcenter from variant popup page viewmodel,subscribed in product page viewmodel constructor. working fine there.when i navigate to the previous page and then came back to this product page for adding one or more variant to the same previously selected product,Messagingcenter subscribe called twice and product value increased twice.Tried to subscribe in the product page onappearing and unsubscribe in disappearing method.still calling two times? How to solve this issue?

calling popup:

var result = await dataService.Get_product_variant(store_id, product_id);
                if (result.status == "success")
                {
                    ind_vis = false;
                    OnPropertyChanged("ind_vis");

                    App.Current.Properties["product_variant_result"] = result;
                    App.Current.Properties["cartitems"] = purchaselist;
                    App.Current.Properties["selected_product"] = product_List2 ;
                    await PopupNavigation.Instance.PushAsync(new Popup_variant());                   
                }

popup viewmodel: sending message

public Popup_variant_vm()
        {
            Radio_btn = new Command<Product_variant_list2>(Radio_stk_tapped);
            product_variant_list = new List<Product_variant_list2>();
            purchaselist = new ObservableCollection<Product_list2>();                       
            show_variants();            
        }

        internal void Confirm_variant()
        {
            if(App.Current.Properties.ContainsKey("selected_variant"))
            {
                 MessagingCenter.Send<Popup_variant_vm, object>(this, "selected_variant", App.Current.Properties["selected_variant"]); //Message send from popup to product page                    
            }
            else
            {
                DependencyService.Get<IToast>().LongAlert("Please select any size");
            }
        }

product page viewmodel: subscribed here..called twice when navigating from previous page to this

public Store_page()
    {
       InitializeComponent();
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();

        var vm = new store_page_vm();
        vm.Navigation = Navigation;
        BindingContext = vm;

        MessagingCenter.Unsubscribe<Popup_variant_vm, object>(this, "selected_variant");
        MessagingCenter.Subscribe<Popup_variant_vm, object>(this, "selected_variant",async (sender, selected_variant) =>
        { 
            var vm1 = BindingContext as store_page_vm;
            vm1?.Addcart2(selected_variant);// called twice
        });            
    } 

unsubscribed in product cs page

protected override void OnDisappearing()
        {
            var vm = BindingContext as store_page_vm;
            vm?.Save_cart();    
            MessagingCenter.Unsubscribe<Popup_variant_vm>(this, "selected_variant");    
        }

Answers

  • VipinKrishnaVipinKrishna Member ✭✭
    edited February 12

    Forgot to add object in unsubscribe..solved

           protected override void OnDisappearing()
                    {
                        var vm = BindingContext as store_page_vm;
                        vm?.Save_cart();    
                        MessagingCenter.Unsubscribe<Popup_variant_vm,object>(this, "selected_variant");    
                    }
    
  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, and thanks for sharing the solution.

Sign In or Register to comment.