Xamarin Forms MessagingCenter Subscribe called two times

VipinKrishnaVipinKrishna Member ✭✭
edited January 24 in Xamarin.Forms

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 navigating from previous page,no issues . After that when i navigate to the next page and then came back to the 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"]);
             //    MessagingCenter.Unsubscribe<Popup_variant_vm>(this, "selected_variant");

            }
            else
            {
                DependencyService.Get<IToast>().LongAlert("Please select any size");
            }
        }

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

public store_page_vm()
        {
            ind_vis = false;
            show_pdt = new Command<Category_data>(Show_product);
            Addtocart = new Command<Product_list2>(Addcart);
            purchaselist = new ObservableCollection<Product_list2>();
            Device.BeginInvokeOnMainThread(Refresh);
            //MessagingCenter.Subscribe<Cart_page>(this, "cart_page_disappear", (sender) =>
            //{
            //    Device.BeginInvokeOnMainThread(Refresh);
            //});
            MessagingCenter.Subscribe<Popup_variant_vm, object>(this, "selected_variant", async (sender, selected_variant) =>
            {
                 var selected_product = (Product_list2)App.Current.Properties["selected_product"];
                selected_product.min_plus_visible = true;
                selected_product.grid_visible = false;
                var selected_variant_size = (Product_variant_list2)selected_variant;
                Product_list2 product_List2 = new Product_list2();
                product_List2.product_id = selected_product.product_id;
                product_List2.id = selected_product.id;
                product_List2.product_name = selected_product.product_name;
                product_List2.no_of_variant = selected_product.no_of_variant;
                product_List2.variant_id = selected_variant_size.id;
                product_List2.variant_name = selected_variant_size.value;
                product_List2.variant_price = selected_variant_size.selling_price; 
                add_to_cart(product_List2);
            });

        }

Tried to subscribe in product cs page onappearing:

public Store_page()
        {
            InitializeComponent();

        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

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

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

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

  • HarshitaHarshita INMember ✭✭✭
    edited January 24

    @VipinKrishna
    You can simply solve this by UnSubscribe the messaging center before subscribe it.Thats it.Its not matter how many time you UnSubscribe this messaging center.

    MessagingCenter.UnSubscribe<Popup_variant_vm, object>(this,"selected_variant");
    MessagingCenter.Subscribe<Popup_variant_vm, object>(this, "selected_variant", async (sender, selected_variant) =>
                {
                     var selected_product = (Product_list2)App.Current.Properties["selected_product"];
                    selected_product.min_plus_visible = true;
                    selected_product.grid_visible = false;
                   ...
               ...
                });
    
            }
    

    and remember that subscribe the messaging center only one place.So I recommand you to subscribe the messaging center where you want(on button click,or any event function).I noticed that you subscribe the messaging center on two places(OnAppearing and Constructor,thats why it calling on both places)

  • VipinKrishnaVipinKrishna Member ✭✭

    @Harshita still calling twice. subscribed in constructor first. then tried in onappearing..

  • VipinKrishnaVipinKrishna Member ✭✭

    @Harshita Thanks. i was unsubscribing like this :
    MessagingCenter.Unsubscribe<Popup_variant_vm>(this, "selected_variant");
    now changed to what you said:
    MessagingCenter.Unsubscribe<Popup_variant_vm, object>(this, "selected_variant");

    now working fine..!

  • 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");    
                }
    
Sign In or Register to comment.