Xamarin Forms MessagingCenter订阅被两次调用

时间:2019-01-24 06:04:32

标签: xamarin xamarin.forms popup messagingcenter

我在产品页面视图模型的列表视图中单击一个产品项,以显示一个弹出窗口(使用rg.plugin弹出窗口)以选择一种产品变体。选择变体后,我将使用来自变体弹出页面视图模型的messagescenter,位于产品页面视图模型构造函数中。当我导航到上一页,然后返回此产品页面以向其中添加一个或多个变体时, 与先前选择的同一产品一样,Messagingcenter订阅两次,产品价值增加了​​两次。尝试在出现的产品页面中订阅,并以消失的方法取消订阅。仍然调用两次吗?如何解决这个问题?

呼叫弹出窗口:

               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());                   
                }

弹出视图模型:发送消息

        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");
            }
        }

产品页面视图模型:在此订阅。从上一页导航到此页面时被调用两次

    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
        });            
    } 

在产品CS页面中取消订阅

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

4 个答案:

答案 0 :(得分:1)

您的退订应如下所示,并且应该可以正常工作:

  MessagingCenter.Unsubscribe<Popup_variant_vm, object>(this, "selected_variant");

答案 1 :(得分:0)

https://www.npmjs.com/package/react-native-swipe-list-view

尝试此操作,并确保您不拨打“订阅”超过一次。

答案 2 :(得分:0)

我的解决方案:

将取消订阅的句子放入订阅正文中!

 MessagingCenter.Subscribe<object, string>(this, "IdSearch", (sender, arg) =>
            {
              

                listView.ItemsSource = arg;

                MessagingCenter.Unsubscribe<object, string>(this, "IdSearch");

            }, BindingContext);

答案 3 :(得分:0)

我在我的应用程序中创建了静态计数器变量,我已经这样做了:


public static class Constants
{
    public static int msgCenterSubscribeCounter { get; set; } = 0;
}

MessagingCenter.Subscribe<object, string>(this, "hello", (sender, arg) =>
{
    Constants.msgCenterSubscribeCounter++;
    if (arg.Equals("hello") && Constants.msgCenterSubscribeCounter == 1)  
    {
        // handle your logic here
    }
});

在调用 Send 的 OnDisappearing() 方法中重置计数器。