Xamarin Forms MessagingCenter Subscribe 调用了两次
Xamarin Forms MessagingCenter Subscribe called two times
我在产品页面视图模型的列表视图中单击一个产品项目以显示弹出窗口(使用 rg.plugin 弹出窗口)以选择其中一个产品 variants.After 选择变体,我正在发送使用消息中心从变体弹出页面视图模型中选择产品页面的变体,在产品页面视图模型构造函数中订阅。工作正常 there.when 我导航到上一页,然后返回到此产品页面以向该产品添加一个或多个变体
之前选择的相同产品,Messagingcenter 订阅调用两次并且产品价值增加 twice.Tried 在产品页面出现时订阅并在消失时取消订阅调用两次?如何解决这个问题?
调用弹出窗口:
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");
}
试试这个,并确保您不会多次调用订阅。
您的退订应该如下所示并且应该有效:
MessagingCenter.Unsubscribe<Popup_variant_vm, object>(this, "selected_variant");
我的解决方案:
将取消订阅语句放入订阅正文!!
MessagingCenter.Subscribe<object, string>(this, "IdSearch", (sender, arg) =>
{
listView.ItemsSource = arg;
MessagingCenter.Unsubscribe<object, string>(this, "IdSearch");
}, BindingContext);
我已经在订阅者的应用程序中创建了静态计数器变量我已经这样做了:
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() 方法中重置计数器。
我在产品页面视图模型的列表视图中单击一个产品项目以显示弹出窗口(使用 rg.plugin 弹出窗口)以选择其中一个产品 variants.After 选择变体,我正在发送使用消息中心从变体弹出页面视图模型中选择产品页面的变体,在产品页面视图模型构造函数中订阅。工作正常 there.when 我导航到上一页,然后返回到此产品页面以向该产品添加一个或多个变体 之前选择的相同产品,Messagingcenter 订阅调用两次并且产品价值增加 twice.Tried 在产品页面出现时订阅并在消失时取消订阅调用两次?如何解决这个问题?
调用弹出窗口:
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");
}
试试这个,并确保您不会多次调用订阅。
您的退订应该如下所示并且应该有效:
MessagingCenter.Unsubscribe<Popup_variant_vm, object>(this, "selected_variant");
我的解决方案:
将取消订阅语句放入订阅正文!!
MessagingCenter.Subscribe<object, string>(this, "IdSearch", (sender, arg) =>
{
listView.ItemsSource = arg;
MessagingCenter.Unsubscribe<object, string>(this, "IdSearch");
}, BindingContext);
我已经在订阅者的应用程序中创建了静态计数器变量我已经这样做了:
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() 方法中重置计数器。