MessagingCenter Unsubscribe not working in xamarin forms when using between viewmodels
I'm working on Xamarin Forms. Unsubscribe is not working in MessagingCenter when using between view models.
I'm trying to unsubscribing messagingCenter on OnDisappear() in ContentPage. The reason why I want to unsubscribing is because when I subscribing, it count as twice and the message also sent twice. It should not receive more than 1 time, same as subscribe too. Below my code.
BottomTabsViewModel where subscribing:
using RCBazaar.Base.ViewModels;
using RCBazaar.Services.Interfaces;
using RCBazaar.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace RCBazaar.ViewModels
{
public class BottomTabsViewModel : ViewModelBase
{
public ICommand HomeCommand => new Command(HomeTab_Clicked);
public ICommand CategoriesCommand => new Command(CategoriesTab_Clicked);
public ICommand CartCommand => new Command(CartTab_Clicked);
public ICommand ChatCommand => new Command(ChatTab_Clicked);
private string _badgeText;
private bool _isActiveHome;
public bool IsActiveHome
{
get
{
return _isActiveHome;
}
set
{
_isActiveHome = value;
RaisePropertyChanged(() => IsActiveHome);
}
}
private bool _isActiveCategory;
public bool IsActiveCategory
{
get
{
return _isActiveCategory;
}
set
{
_isActiveCategory = value;
RaisePropertyChanged(() => IsActiveCategory);
}
}
private bool _isActiveCart;
public bool IsActiveCart
{
get
{
return _isActiveCart;
}
set
{
_isActiveCart = value;
RaisePropertyChanged(() => IsActiveCart);
}
}
private bool _isActiveChat;
public bool IsActiveChat
{
get
{
return _isActiveChat;
}
set
{
_isActiveChat = value;
RaisePropertyChanged(() => IsActiveChat);
}
}
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public static string IsSelected { get; set; }
public BottomTabsViewModel()
{
BadgeCount();
if (IsSelected == "Home")
{
ActiveHome();
}
else if (IsSelected == "Category")
{
ActiveCategory();
}
else if (IsSelected == "Cart")
{
ActiveCart();
}
else if (IsSelected == "Chat")
{
ActiveChat();
}
else
{
ActiveHome();
}
}
private void BadgeCount()
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount", (sender, arg) =>
{
try
{
if (arg != null)
{
BadgeText = arg;
}
}
catch (Exception ex)
{
}
});
}
public void HomeTab_Clicked()
{
IsSelected = "Home";
NavigationService.NavigateToAsync<HomeProductsViewModel>();
}
public void CategoriesTab_Clicked()
{
IsSelected = "Category";
NavigationService.NavigateToAsync<MainCategoriesListViewModel>();
}
public void CartTab_Clicked()
{
IsSelected = "Cart";
NavigationService.NavigateToAsync<AccordianViewModel>();
}
public void ChatTab_Clicked()
{
IsSelected = "Chat";
NavigationService.NavigateToAsync<RCBazaarInformationViewModel>("Contact us");
}
public void ActiveHome()
{
IsActiveHome = true;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCategory()
{
IsActiveHome = false;
IsActiveCategory = true;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCart()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = true;
IsActiveChat = false;
}
public void ActiveChat()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = true;
}
}
}
** ProductDetailViewModel where sending message:**
using FFImageLoading.Forms;
using Plugin.Share;
using Plugin.Share.Abstractions;
using RCBazaar.Base.ViewModels;
using RCBazaar.Model.ShoppingCart;
using RCBazaar.Services.Interfaces;
using RCBazaar.Services.Products;
using RCBazaar.ViewModels;
using RCBazaar.Views;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
[assembly: Dependency(typeof(ProductDetailsViewModel))]
namespace RCBazaar.ViewModels
{
public class ProductDetailsViewModel : ViewModelBase
{
private Product _productDetails;
private string _productName;
private long _productStock;
private double _productPrice;
private string _productDescription;
private ObservableCollection<string> _productImage;
private string _productSKU;
private ObservableCollection<CartItem> _basketItems;
private Color _badgeColor;
private long _productId;
private string _overviewColor;
private string _reviewsColor;
private bool _productDescriptionIsVisible;
private long _productRating;
private string _badgeText;
public ProductDetailsViewModel()
{
try
{
BasketItems = new ObservableCollection<CartItem>();
BadgeText = "0";
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
// _navigation = navigation;
var images = new List<string>() { "CourselStaticImage3.jpeg", "CourselStaticImage2.jpeg", "CourselStaticImage1.jpeg", "CourselStaticImage4.jpeg" };
ProductImage = new ObservableCollection<string>(images);
OnRatingChanged();
MyCommand = new Command(() =>
{
Debug.WriteLine("Position selected.");
});
int numbers = new int { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<WareHouse> wareHouses = new List<WareHouse>()
{
new WareHouse {WarehouseId=1, WareHouseName= "Banglore outlet", Items= numbers },
new WareHouse {WarehouseId=2, WareHouseName= "Pune outlet", Items= numbers },
new WareHouse {WarehouseId=3, WareHouseName= "Mumbai outlet", Items= numbers }
};
ListHeight = wareHouses.Count * 30;
WareHouses = new ObservableCollection<WareHouse>(wareHouses);
Reviews = new ObservableCollection<Review> { new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
};
}
catch (Exception ex)
{
}
}
public class Review
{
public string id { get; set; }
public string ReviewTitle { get; set; }
public string ReviewText { get; set; }
public float rating { get; set; }
public string CreatedDate { get; set; }
public bool IsReviewHelpful { get; set; }
}
private ObservableCollection<Review> _reviews;
public ObservableCollection<Review> Reviews
{
get
{
return _reviews;
}
set
{
_reviews = value;
RaisePropertyChanged(() => Reviews);
}
}
public override async Task InitializeAsync(object navigationData)
{
_productDetails = (Product)navigationData;
ProductName = _productDetails.Name;
ProductStock = _productDetails.Qtyonhand;
ProductPrice = _productDetails.pricelist;
ProductDescription = _productDetails.FullDescription;
ProductRating = _productDetails.RatingSum;
// ProductImage = _productDetails.PictureBinary;
ProductId = _productDetails.ProductId;
ProductSKU = _productDetails.upc;
//// Update Basket/badgecount
//var basket = await _basketService.GetBasketAsync(userInfo.UserId, authToken);
//if (basket != null && basket.Items != null && basket.Items.Any())
//{
// BadgeCount = 0;
// BasketItems.Clear();
// foreach (var basketItem in basket.Items)
// {
// BadgeCount += basketItem.Quantity;
// await AddBasketItemAsync(basketItem);
// }
//}
}
//private async Task AddBasketItemAsync(BasketItem item)
//{
// BasketItems.Add(item);
// await ReCalculateTotalAsync();
//}
public ICommand ItemSelectedCommand => new Command(async () =>
{
try
{
WarehouseItemSelected = null;
}
catch (Exception ex)
{
}
});
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public ICommand AddtoCartCommand => new Command(async(e)=> await AddItemAsync(e));
private async Task AddItemAsync(object e)
{
try
{
var value = int.Parse(BadgeText);
if (value < 10)
{
BadgeText = (++value).ToString();
//BadgeColor = Color.FromHex("#000000");
//await Task.Delay(3000);
//BadgeColor = Color.FromHex("#FF9933");
}
else
BadgeText = "0";
MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
var selectedItem = (ProductDetailsViewModel)e;
await AddBasketItemAsync(selectedItem);
RaisePropertyChanged(() => BasketItems);
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
public Color BadgeColor
{
get
{
return _badgeColor;
}
set
{
_badgeColor = value;
RaisePropertyChanged(() => BadgeColor);
}
}
private async Task AddBasketItemAsync(ProductDetailsViewModel selectedItem)
{
try
{
// BasketItems.Add(selectedItem); this can be used if BasketItems is of type productdetauilsviewmodel
BasketItems.Add(new CartItem { BasketProductId = ProductId, WarehouseDetails = WareHouses });
await ReCalculateTotalAsync();
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
private async Task ReCalculateTotalAsync()
{
//Total = 0;
//if (BasketItems == null)
//{
// return;
//}
//foreach (var orderItem in BasketItems)
//{
// Total += (orderItem.Quantity * orderItem.UnitPrice);
//}
//var authToken = _settingsService.AuthAccessToken;
//var userInfo = await _userService.GetUserInfoAsync(authToken);
//await _basketService.UpdateBasketAsync(new CustomerBasket
//{
// BuyerId = userInfo.UserId,
// Items = BasketItems.ToList()
//}, authToken);
}
//public ICommand AddtoCartCommand => new Command(async () =>
//{
// var value = int.Parse(BadgeText);
// if (value < 10)
// BadgeText = (++value).ToString();
// else
// BadgeText = "0";
// MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
//});
public ObservableCollection<CartItem> BasketItems
{
get { return _basketItems; }
set
{
_basketItems = value;
RaisePropertyChanged(() => BasketItems);
}
}
private int _listHeight;
public int ListHeight
{
get
{
return _listHeight;
}
set
{
_listHeight = value;
RaisePropertyChanged(() => ListHeight);
}
}
private WareHouse _warehouseItemSelected;
public WareHouse WarehouseItemSelected
{
get
{
return _warehouseItemSelected;
}
set
{
_warehouseItemSelected = value;
RaisePropertyChanged(() => WarehouseItemSelected);
}
}
public class WareHouse
{
public int WarehouseId{ get; set; }
public string WareHouseName { get; set; }
public int Items { get; set; }
public int quantity { get; set; }
}
private ObservableCollection<WareHouse> _wareHouses;
public ObservableCollection<WareHouse> WareHouses
{
get
{
return _wareHouses;
}
set
{
_wareHouses = value;
RaisePropertyChanged(() => WareHouses);
}
}
public long ProductRating
{
get
{
return _productRating;
}
set
{
_productRating = value;
RaisePropertyChanged(() => ProductRating);
}
}
public long ProductId
{
get
{
return _productId;
}
set
{
_productId = value;
RaisePropertyChanged(() => ProductId);
}
}
public ObservableCollection<string> ProductImage
{
get
{
return _productImage;
}
set
{
_productImage = value;
RaisePropertyChanged(() => ProductImage);
}
}
public string ProductName
{
get
{
return _productName;
}
set
{
_productName = value;
RaisePropertyChanged(() => ProductName);
}
}
public long ProductStock
{
get
{
return _productStock;
}
set
{
_productStock = value;
RaisePropertyChanged(() => ProductStock);
}
}
public double ProductPrice
{
get
{
return _productPrice;
}
set
{
_productPrice = value;
RaisePropertyChanged(() => ProductPrice);
}
}
public string ProductSKU
{
get
{
return _productSKU;
}
set
{
_productSKU = value;
RaisePropertyChanged(() => ProductSKU);
}
}
public string OverviewColor
{
get
{
return _overviewColor;
}
set
{
_overviewColor = value;
RaisePropertyChanged(() => OverviewColor);
}
}
public string ReviewsColor
{
get
{
return _reviewsColor;
}
set
{
_reviewsColor = value;
RaisePropertyChanged(() => ReviewsColor);
}
}
public string ProductDescription
{
get
{
return _productDescription;
}
set
{
_productDescription = value;
RaisePropertyChanged(() => ProductDescription);
}
}
public bool ProductDescriptionIsVisible
{
get
{
return _productDescriptionIsVisible;
}
set
{
_productDescriptionIsVisible = value;
RaisePropertyChanged(() => ProductDescriptionIsVisible);
}
}
private bool _reviewIsVisible;
public bool ReviewIsVisible
{
get
{
return _reviewIsVisible;
}
set
{
_reviewIsVisible = value;
RaisePropertyChanged(() => ReviewIsVisible);
}
}
public Command MyCommand { protected set; get; }
public ICommand AddReviewCommand => new Command(async () =>
{
try
{
// await push.Instance.PopAllAsync();
await PopupNavigation.Instance.PushAsync(new ReviewPopUpView());
//await _navigation.PushAsync(new ReviewPopUpView());
}
catch (Exception ex)
{
}
});
public ICommand ShareCommand => new Command(async () =>
{
try
{
await CrossShare.Current.Share(new ShareMessage
{
Text = "Please download RcBazaar app from playstore",
Title = "Share Product",
Url = "http://rcbazaar.com/product.aspx?productid=6863"
});
}
catch (Exception ex)
{
}
});
private void OnRatingChanged()
{
try
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductsView, float>(this, "ProductsPage", (sender, arg) =>
{
if (arg != null)
{
Rating = arg;
}
});
}
catch (Exception ex)
{
}
}
private float _rating;
public float Rating
{
get
{
return _rating;
}
set
{
_rating = value;
RaisePropertyChanged(() => Rating);
}
}
public ICommand ViewImageCommand => new Command(async (parameter) => await ViewImage((string)parameter));
public async Task ViewImage(string ImageName)
{
try
{
await NavigationService.NavigateToAsync<ImageFullScreenViewModel>(ImageName);
}
catch (Exception ex)
{
}
}
public ICommand OverviewCommand => new Command(async () =>
{
try
{
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
}
catch (Exception ex)
{
}
});
public ICommand ReviewsCommand => new Command(async () =>
{
try
{
OverviewColor = "#DCDCDC";//lightgray
ReviewsColor = "#231f20";//blue color
ProductDescriptionIsVisible = false;
ReviewIsVisible = true;
}
catch (Exception ex)
{
}
});
}
}
Unsubscribing when home page disappears:
using FFImageLoading.Work;
using RCBazaar.CustomRenderer;
using RCBazaar.Helpers;
using RCBazaar.ViewModels;
using RCBazaar.Views.NavigationBarWithSideDrawer;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace RCBazaar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomeView : ContentPage
{
public HomeView()
{
try
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
//BindingContext = new HomeProductsViewModel(this.Navigation);
}
catch (Exception ex)
{
App.Current.MainPage.DisplayAlert(Constants.ConstantKeys.Alert, Constants.ConstantKeys.UIError, Constants.ConstantKeys.Ok);
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
}
}
c# android xaml xamarin xamarin.forms
add a comment |
I'm working on Xamarin Forms. Unsubscribe is not working in MessagingCenter when using between view models.
I'm trying to unsubscribing messagingCenter on OnDisappear() in ContentPage. The reason why I want to unsubscribing is because when I subscribing, it count as twice and the message also sent twice. It should not receive more than 1 time, same as subscribe too. Below my code.
BottomTabsViewModel where subscribing:
using RCBazaar.Base.ViewModels;
using RCBazaar.Services.Interfaces;
using RCBazaar.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace RCBazaar.ViewModels
{
public class BottomTabsViewModel : ViewModelBase
{
public ICommand HomeCommand => new Command(HomeTab_Clicked);
public ICommand CategoriesCommand => new Command(CategoriesTab_Clicked);
public ICommand CartCommand => new Command(CartTab_Clicked);
public ICommand ChatCommand => new Command(ChatTab_Clicked);
private string _badgeText;
private bool _isActiveHome;
public bool IsActiveHome
{
get
{
return _isActiveHome;
}
set
{
_isActiveHome = value;
RaisePropertyChanged(() => IsActiveHome);
}
}
private bool _isActiveCategory;
public bool IsActiveCategory
{
get
{
return _isActiveCategory;
}
set
{
_isActiveCategory = value;
RaisePropertyChanged(() => IsActiveCategory);
}
}
private bool _isActiveCart;
public bool IsActiveCart
{
get
{
return _isActiveCart;
}
set
{
_isActiveCart = value;
RaisePropertyChanged(() => IsActiveCart);
}
}
private bool _isActiveChat;
public bool IsActiveChat
{
get
{
return _isActiveChat;
}
set
{
_isActiveChat = value;
RaisePropertyChanged(() => IsActiveChat);
}
}
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public static string IsSelected { get; set; }
public BottomTabsViewModel()
{
BadgeCount();
if (IsSelected == "Home")
{
ActiveHome();
}
else if (IsSelected == "Category")
{
ActiveCategory();
}
else if (IsSelected == "Cart")
{
ActiveCart();
}
else if (IsSelected == "Chat")
{
ActiveChat();
}
else
{
ActiveHome();
}
}
private void BadgeCount()
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount", (sender, arg) =>
{
try
{
if (arg != null)
{
BadgeText = arg;
}
}
catch (Exception ex)
{
}
});
}
public void HomeTab_Clicked()
{
IsSelected = "Home";
NavigationService.NavigateToAsync<HomeProductsViewModel>();
}
public void CategoriesTab_Clicked()
{
IsSelected = "Category";
NavigationService.NavigateToAsync<MainCategoriesListViewModel>();
}
public void CartTab_Clicked()
{
IsSelected = "Cart";
NavigationService.NavigateToAsync<AccordianViewModel>();
}
public void ChatTab_Clicked()
{
IsSelected = "Chat";
NavigationService.NavigateToAsync<RCBazaarInformationViewModel>("Contact us");
}
public void ActiveHome()
{
IsActiveHome = true;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCategory()
{
IsActiveHome = false;
IsActiveCategory = true;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCart()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = true;
IsActiveChat = false;
}
public void ActiveChat()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = true;
}
}
}
** ProductDetailViewModel where sending message:**
using FFImageLoading.Forms;
using Plugin.Share;
using Plugin.Share.Abstractions;
using RCBazaar.Base.ViewModels;
using RCBazaar.Model.ShoppingCart;
using RCBazaar.Services.Interfaces;
using RCBazaar.Services.Products;
using RCBazaar.ViewModels;
using RCBazaar.Views;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
[assembly: Dependency(typeof(ProductDetailsViewModel))]
namespace RCBazaar.ViewModels
{
public class ProductDetailsViewModel : ViewModelBase
{
private Product _productDetails;
private string _productName;
private long _productStock;
private double _productPrice;
private string _productDescription;
private ObservableCollection<string> _productImage;
private string _productSKU;
private ObservableCollection<CartItem> _basketItems;
private Color _badgeColor;
private long _productId;
private string _overviewColor;
private string _reviewsColor;
private bool _productDescriptionIsVisible;
private long _productRating;
private string _badgeText;
public ProductDetailsViewModel()
{
try
{
BasketItems = new ObservableCollection<CartItem>();
BadgeText = "0";
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
// _navigation = navigation;
var images = new List<string>() { "CourselStaticImage3.jpeg", "CourselStaticImage2.jpeg", "CourselStaticImage1.jpeg", "CourselStaticImage4.jpeg" };
ProductImage = new ObservableCollection<string>(images);
OnRatingChanged();
MyCommand = new Command(() =>
{
Debug.WriteLine("Position selected.");
});
int numbers = new int { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<WareHouse> wareHouses = new List<WareHouse>()
{
new WareHouse {WarehouseId=1, WareHouseName= "Banglore outlet", Items= numbers },
new WareHouse {WarehouseId=2, WareHouseName= "Pune outlet", Items= numbers },
new WareHouse {WarehouseId=3, WareHouseName= "Mumbai outlet", Items= numbers }
};
ListHeight = wareHouses.Count * 30;
WareHouses = new ObservableCollection<WareHouse>(wareHouses);
Reviews = new ObservableCollection<Review> { new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
};
}
catch (Exception ex)
{
}
}
public class Review
{
public string id { get; set; }
public string ReviewTitle { get; set; }
public string ReviewText { get; set; }
public float rating { get; set; }
public string CreatedDate { get; set; }
public bool IsReviewHelpful { get; set; }
}
private ObservableCollection<Review> _reviews;
public ObservableCollection<Review> Reviews
{
get
{
return _reviews;
}
set
{
_reviews = value;
RaisePropertyChanged(() => Reviews);
}
}
public override async Task InitializeAsync(object navigationData)
{
_productDetails = (Product)navigationData;
ProductName = _productDetails.Name;
ProductStock = _productDetails.Qtyonhand;
ProductPrice = _productDetails.pricelist;
ProductDescription = _productDetails.FullDescription;
ProductRating = _productDetails.RatingSum;
// ProductImage = _productDetails.PictureBinary;
ProductId = _productDetails.ProductId;
ProductSKU = _productDetails.upc;
//// Update Basket/badgecount
//var basket = await _basketService.GetBasketAsync(userInfo.UserId, authToken);
//if (basket != null && basket.Items != null && basket.Items.Any())
//{
// BadgeCount = 0;
// BasketItems.Clear();
// foreach (var basketItem in basket.Items)
// {
// BadgeCount += basketItem.Quantity;
// await AddBasketItemAsync(basketItem);
// }
//}
}
//private async Task AddBasketItemAsync(BasketItem item)
//{
// BasketItems.Add(item);
// await ReCalculateTotalAsync();
//}
public ICommand ItemSelectedCommand => new Command(async () =>
{
try
{
WarehouseItemSelected = null;
}
catch (Exception ex)
{
}
});
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public ICommand AddtoCartCommand => new Command(async(e)=> await AddItemAsync(e));
private async Task AddItemAsync(object e)
{
try
{
var value = int.Parse(BadgeText);
if (value < 10)
{
BadgeText = (++value).ToString();
//BadgeColor = Color.FromHex("#000000");
//await Task.Delay(3000);
//BadgeColor = Color.FromHex("#FF9933");
}
else
BadgeText = "0";
MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
var selectedItem = (ProductDetailsViewModel)e;
await AddBasketItemAsync(selectedItem);
RaisePropertyChanged(() => BasketItems);
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
public Color BadgeColor
{
get
{
return _badgeColor;
}
set
{
_badgeColor = value;
RaisePropertyChanged(() => BadgeColor);
}
}
private async Task AddBasketItemAsync(ProductDetailsViewModel selectedItem)
{
try
{
// BasketItems.Add(selectedItem); this can be used if BasketItems is of type productdetauilsviewmodel
BasketItems.Add(new CartItem { BasketProductId = ProductId, WarehouseDetails = WareHouses });
await ReCalculateTotalAsync();
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
private async Task ReCalculateTotalAsync()
{
//Total = 0;
//if (BasketItems == null)
//{
// return;
//}
//foreach (var orderItem in BasketItems)
//{
// Total += (orderItem.Quantity * orderItem.UnitPrice);
//}
//var authToken = _settingsService.AuthAccessToken;
//var userInfo = await _userService.GetUserInfoAsync(authToken);
//await _basketService.UpdateBasketAsync(new CustomerBasket
//{
// BuyerId = userInfo.UserId,
// Items = BasketItems.ToList()
//}, authToken);
}
//public ICommand AddtoCartCommand => new Command(async () =>
//{
// var value = int.Parse(BadgeText);
// if (value < 10)
// BadgeText = (++value).ToString();
// else
// BadgeText = "0";
// MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
//});
public ObservableCollection<CartItem> BasketItems
{
get { return _basketItems; }
set
{
_basketItems = value;
RaisePropertyChanged(() => BasketItems);
}
}
private int _listHeight;
public int ListHeight
{
get
{
return _listHeight;
}
set
{
_listHeight = value;
RaisePropertyChanged(() => ListHeight);
}
}
private WareHouse _warehouseItemSelected;
public WareHouse WarehouseItemSelected
{
get
{
return _warehouseItemSelected;
}
set
{
_warehouseItemSelected = value;
RaisePropertyChanged(() => WarehouseItemSelected);
}
}
public class WareHouse
{
public int WarehouseId{ get; set; }
public string WareHouseName { get; set; }
public int Items { get; set; }
public int quantity { get; set; }
}
private ObservableCollection<WareHouse> _wareHouses;
public ObservableCollection<WareHouse> WareHouses
{
get
{
return _wareHouses;
}
set
{
_wareHouses = value;
RaisePropertyChanged(() => WareHouses);
}
}
public long ProductRating
{
get
{
return _productRating;
}
set
{
_productRating = value;
RaisePropertyChanged(() => ProductRating);
}
}
public long ProductId
{
get
{
return _productId;
}
set
{
_productId = value;
RaisePropertyChanged(() => ProductId);
}
}
public ObservableCollection<string> ProductImage
{
get
{
return _productImage;
}
set
{
_productImage = value;
RaisePropertyChanged(() => ProductImage);
}
}
public string ProductName
{
get
{
return _productName;
}
set
{
_productName = value;
RaisePropertyChanged(() => ProductName);
}
}
public long ProductStock
{
get
{
return _productStock;
}
set
{
_productStock = value;
RaisePropertyChanged(() => ProductStock);
}
}
public double ProductPrice
{
get
{
return _productPrice;
}
set
{
_productPrice = value;
RaisePropertyChanged(() => ProductPrice);
}
}
public string ProductSKU
{
get
{
return _productSKU;
}
set
{
_productSKU = value;
RaisePropertyChanged(() => ProductSKU);
}
}
public string OverviewColor
{
get
{
return _overviewColor;
}
set
{
_overviewColor = value;
RaisePropertyChanged(() => OverviewColor);
}
}
public string ReviewsColor
{
get
{
return _reviewsColor;
}
set
{
_reviewsColor = value;
RaisePropertyChanged(() => ReviewsColor);
}
}
public string ProductDescription
{
get
{
return _productDescription;
}
set
{
_productDescription = value;
RaisePropertyChanged(() => ProductDescription);
}
}
public bool ProductDescriptionIsVisible
{
get
{
return _productDescriptionIsVisible;
}
set
{
_productDescriptionIsVisible = value;
RaisePropertyChanged(() => ProductDescriptionIsVisible);
}
}
private bool _reviewIsVisible;
public bool ReviewIsVisible
{
get
{
return _reviewIsVisible;
}
set
{
_reviewIsVisible = value;
RaisePropertyChanged(() => ReviewIsVisible);
}
}
public Command MyCommand { protected set; get; }
public ICommand AddReviewCommand => new Command(async () =>
{
try
{
// await push.Instance.PopAllAsync();
await PopupNavigation.Instance.PushAsync(new ReviewPopUpView());
//await _navigation.PushAsync(new ReviewPopUpView());
}
catch (Exception ex)
{
}
});
public ICommand ShareCommand => new Command(async () =>
{
try
{
await CrossShare.Current.Share(new ShareMessage
{
Text = "Please download RcBazaar app from playstore",
Title = "Share Product",
Url = "http://rcbazaar.com/product.aspx?productid=6863"
});
}
catch (Exception ex)
{
}
});
private void OnRatingChanged()
{
try
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductsView, float>(this, "ProductsPage", (sender, arg) =>
{
if (arg != null)
{
Rating = arg;
}
});
}
catch (Exception ex)
{
}
}
private float _rating;
public float Rating
{
get
{
return _rating;
}
set
{
_rating = value;
RaisePropertyChanged(() => Rating);
}
}
public ICommand ViewImageCommand => new Command(async (parameter) => await ViewImage((string)parameter));
public async Task ViewImage(string ImageName)
{
try
{
await NavigationService.NavigateToAsync<ImageFullScreenViewModel>(ImageName);
}
catch (Exception ex)
{
}
}
public ICommand OverviewCommand => new Command(async () =>
{
try
{
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
}
catch (Exception ex)
{
}
});
public ICommand ReviewsCommand => new Command(async () =>
{
try
{
OverviewColor = "#DCDCDC";//lightgray
ReviewsColor = "#231f20";//blue color
ProductDescriptionIsVisible = false;
ReviewIsVisible = true;
}
catch (Exception ex)
{
}
});
}
}
Unsubscribing when home page disappears:
using FFImageLoading.Work;
using RCBazaar.CustomRenderer;
using RCBazaar.Helpers;
using RCBazaar.ViewModels;
using RCBazaar.Views.NavigationBarWithSideDrawer;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace RCBazaar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomeView : ContentPage
{
public HomeView()
{
try
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
//BindingContext = new HomeProductsViewModel(this.Navigation);
}
catch (Exception ex)
{
App.Current.MainPage.DisplayAlert(Constants.ConstantKeys.Alert, Constants.ConstantKeys.UIError, Constants.ConstantKeys.Ok);
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
}
}
c# android xaml xamarin xamarin.forms
When do you subscribe?
– EvZ
Nov 20 '18 at 10:56
Its subscribing In viewModel. There is a bottom view in a page. ControlTemplate through showing the view and that view has view model. There it is subscribing.
– praveena H M
Nov 20 '18 at 11:33
I would recommend to identify the problem that is causing multiple subscriptions at first place, however without seeing the code is a bit hard.
– EvZ
Nov 20 '18 at 11:37
yeah i wil put the classes right now.Means i will update my question.Please wait.
– praveena H M
Nov 20 '18 at 11:58
add a comment |
I'm working on Xamarin Forms. Unsubscribe is not working in MessagingCenter when using between view models.
I'm trying to unsubscribing messagingCenter on OnDisappear() in ContentPage. The reason why I want to unsubscribing is because when I subscribing, it count as twice and the message also sent twice. It should not receive more than 1 time, same as subscribe too. Below my code.
BottomTabsViewModel where subscribing:
using RCBazaar.Base.ViewModels;
using RCBazaar.Services.Interfaces;
using RCBazaar.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace RCBazaar.ViewModels
{
public class BottomTabsViewModel : ViewModelBase
{
public ICommand HomeCommand => new Command(HomeTab_Clicked);
public ICommand CategoriesCommand => new Command(CategoriesTab_Clicked);
public ICommand CartCommand => new Command(CartTab_Clicked);
public ICommand ChatCommand => new Command(ChatTab_Clicked);
private string _badgeText;
private bool _isActiveHome;
public bool IsActiveHome
{
get
{
return _isActiveHome;
}
set
{
_isActiveHome = value;
RaisePropertyChanged(() => IsActiveHome);
}
}
private bool _isActiveCategory;
public bool IsActiveCategory
{
get
{
return _isActiveCategory;
}
set
{
_isActiveCategory = value;
RaisePropertyChanged(() => IsActiveCategory);
}
}
private bool _isActiveCart;
public bool IsActiveCart
{
get
{
return _isActiveCart;
}
set
{
_isActiveCart = value;
RaisePropertyChanged(() => IsActiveCart);
}
}
private bool _isActiveChat;
public bool IsActiveChat
{
get
{
return _isActiveChat;
}
set
{
_isActiveChat = value;
RaisePropertyChanged(() => IsActiveChat);
}
}
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public static string IsSelected { get; set; }
public BottomTabsViewModel()
{
BadgeCount();
if (IsSelected == "Home")
{
ActiveHome();
}
else if (IsSelected == "Category")
{
ActiveCategory();
}
else if (IsSelected == "Cart")
{
ActiveCart();
}
else if (IsSelected == "Chat")
{
ActiveChat();
}
else
{
ActiveHome();
}
}
private void BadgeCount()
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount", (sender, arg) =>
{
try
{
if (arg != null)
{
BadgeText = arg;
}
}
catch (Exception ex)
{
}
});
}
public void HomeTab_Clicked()
{
IsSelected = "Home";
NavigationService.NavigateToAsync<HomeProductsViewModel>();
}
public void CategoriesTab_Clicked()
{
IsSelected = "Category";
NavigationService.NavigateToAsync<MainCategoriesListViewModel>();
}
public void CartTab_Clicked()
{
IsSelected = "Cart";
NavigationService.NavigateToAsync<AccordianViewModel>();
}
public void ChatTab_Clicked()
{
IsSelected = "Chat";
NavigationService.NavigateToAsync<RCBazaarInformationViewModel>("Contact us");
}
public void ActiveHome()
{
IsActiveHome = true;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCategory()
{
IsActiveHome = false;
IsActiveCategory = true;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCart()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = true;
IsActiveChat = false;
}
public void ActiveChat()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = true;
}
}
}
** ProductDetailViewModel where sending message:**
using FFImageLoading.Forms;
using Plugin.Share;
using Plugin.Share.Abstractions;
using RCBazaar.Base.ViewModels;
using RCBazaar.Model.ShoppingCart;
using RCBazaar.Services.Interfaces;
using RCBazaar.Services.Products;
using RCBazaar.ViewModels;
using RCBazaar.Views;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
[assembly: Dependency(typeof(ProductDetailsViewModel))]
namespace RCBazaar.ViewModels
{
public class ProductDetailsViewModel : ViewModelBase
{
private Product _productDetails;
private string _productName;
private long _productStock;
private double _productPrice;
private string _productDescription;
private ObservableCollection<string> _productImage;
private string _productSKU;
private ObservableCollection<CartItem> _basketItems;
private Color _badgeColor;
private long _productId;
private string _overviewColor;
private string _reviewsColor;
private bool _productDescriptionIsVisible;
private long _productRating;
private string _badgeText;
public ProductDetailsViewModel()
{
try
{
BasketItems = new ObservableCollection<CartItem>();
BadgeText = "0";
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
// _navigation = navigation;
var images = new List<string>() { "CourselStaticImage3.jpeg", "CourselStaticImage2.jpeg", "CourselStaticImage1.jpeg", "CourselStaticImage4.jpeg" };
ProductImage = new ObservableCollection<string>(images);
OnRatingChanged();
MyCommand = new Command(() =>
{
Debug.WriteLine("Position selected.");
});
int numbers = new int { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<WareHouse> wareHouses = new List<WareHouse>()
{
new WareHouse {WarehouseId=1, WareHouseName= "Banglore outlet", Items= numbers },
new WareHouse {WarehouseId=2, WareHouseName= "Pune outlet", Items= numbers },
new WareHouse {WarehouseId=3, WareHouseName= "Mumbai outlet", Items= numbers }
};
ListHeight = wareHouses.Count * 30;
WareHouses = new ObservableCollection<WareHouse>(wareHouses);
Reviews = new ObservableCollection<Review> { new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
};
}
catch (Exception ex)
{
}
}
public class Review
{
public string id { get; set; }
public string ReviewTitle { get; set; }
public string ReviewText { get; set; }
public float rating { get; set; }
public string CreatedDate { get; set; }
public bool IsReviewHelpful { get; set; }
}
private ObservableCollection<Review> _reviews;
public ObservableCollection<Review> Reviews
{
get
{
return _reviews;
}
set
{
_reviews = value;
RaisePropertyChanged(() => Reviews);
}
}
public override async Task InitializeAsync(object navigationData)
{
_productDetails = (Product)navigationData;
ProductName = _productDetails.Name;
ProductStock = _productDetails.Qtyonhand;
ProductPrice = _productDetails.pricelist;
ProductDescription = _productDetails.FullDescription;
ProductRating = _productDetails.RatingSum;
// ProductImage = _productDetails.PictureBinary;
ProductId = _productDetails.ProductId;
ProductSKU = _productDetails.upc;
//// Update Basket/badgecount
//var basket = await _basketService.GetBasketAsync(userInfo.UserId, authToken);
//if (basket != null && basket.Items != null && basket.Items.Any())
//{
// BadgeCount = 0;
// BasketItems.Clear();
// foreach (var basketItem in basket.Items)
// {
// BadgeCount += basketItem.Quantity;
// await AddBasketItemAsync(basketItem);
// }
//}
}
//private async Task AddBasketItemAsync(BasketItem item)
//{
// BasketItems.Add(item);
// await ReCalculateTotalAsync();
//}
public ICommand ItemSelectedCommand => new Command(async () =>
{
try
{
WarehouseItemSelected = null;
}
catch (Exception ex)
{
}
});
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public ICommand AddtoCartCommand => new Command(async(e)=> await AddItemAsync(e));
private async Task AddItemAsync(object e)
{
try
{
var value = int.Parse(BadgeText);
if (value < 10)
{
BadgeText = (++value).ToString();
//BadgeColor = Color.FromHex("#000000");
//await Task.Delay(3000);
//BadgeColor = Color.FromHex("#FF9933");
}
else
BadgeText = "0";
MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
var selectedItem = (ProductDetailsViewModel)e;
await AddBasketItemAsync(selectedItem);
RaisePropertyChanged(() => BasketItems);
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
public Color BadgeColor
{
get
{
return _badgeColor;
}
set
{
_badgeColor = value;
RaisePropertyChanged(() => BadgeColor);
}
}
private async Task AddBasketItemAsync(ProductDetailsViewModel selectedItem)
{
try
{
// BasketItems.Add(selectedItem); this can be used if BasketItems is of type productdetauilsviewmodel
BasketItems.Add(new CartItem { BasketProductId = ProductId, WarehouseDetails = WareHouses });
await ReCalculateTotalAsync();
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
private async Task ReCalculateTotalAsync()
{
//Total = 0;
//if (BasketItems == null)
//{
// return;
//}
//foreach (var orderItem in BasketItems)
//{
// Total += (orderItem.Quantity * orderItem.UnitPrice);
//}
//var authToken = _settingsService.AuthAccessToken;
//var userInfo = await _userService.GetUserInfoAsync(authToken);
//await _basketService.UpdateBasketAsync(new CustomerBasket
//{
// BuyerId = userInfo.UserId,
// Items = BasketItems.ToList()
//}, authToken);
}
//public ICommand AddtoCartCommand => new Command(async () =>
//{
// var value = int.Parse(BadgeText);
// if (value < 10)
// BadgeText = (++value).ToString();
// else
// BadgeText = "0";
// MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
//});
public ObservableCollection<CartItem> BasketItems
{
get { return _basketItems; }
set
{
_basketItems = value;
RaisePropertyChanged(() => BasketItems);
}
}
private int _listHeight;
public int ListHeight
{
get
{
return _listHeight;
}
set
{
_listHeight = value;
RaisePropertyChanged(() => ListHeight);
}
}
private WareHouse _warehouseItemSelected;
public WareHouse WarehouseItemSelected
{
get
{
return _warehouseItemSelected;
}
set
{
_warehouseItemSelected = value;
RaisePropertyChanged(() => WarehouseItemSelected);
}
}
public class WareHouse
{
public int WarehouseId{ get; set; }
public string WareHouseName { get; set; }
public int Items { get; set; }
public int quantity { get; set; }
}
private ObservableCollection<WareHouse> _wareHouses;
public ObservableCollection<WareHouse> WareHouses
{
get
{
return _wareHouses;
}
set
{
_wareHouses = value;
RaisePropertyChanged(() => WareHouses);
}
}
public long ProductRating
{
get
{
return _productRating;
}
set
{
_productRating = value;
RaisePropertyChanged(() => ProductRating);
}
}
public long ProductId
{
get
{
return _productId;
}
set
{
_productId = value;
RaisePropertyChanged(() => ProductId);
}
}
public ObservableCollection<string> ProductImage
{
get
{
return _productImage;
}
set
{
_productImage = value;
RaisePropertyChanged(() => ProductImage);
}
}
public string ProductName
{
get
{
return _productName;
}
set
{
_productName = value;
RaisePropertyChanged(() => ProductName);
}
}
public long ProductStock
{
get
{
return _productStock;
}
set
{
_productStock = value;
RaisePropertyChanged(() => ProductStock);
}
}
public double ProductPrice
{
get
{
return _productPrice;
}
set
{
_productPrice = value;
RaisePropertyChanged(() => ProductPrice);
}
}
public string ProductSKU
{
get
{
return _productSKU;
}
set
{
_productSKU = value;
RaisePropertyChanged(() => ProductSKU);
}
}
public string OverviewColor
{
get
{
return _overviewColor;
}
set
{
_overviewColor = value;
RaisePropertyChanged(() => OverviewColor);
}
}
public string ReviewsColor
{
get
{
return _reviewsColor;
}
set
{
_reviewsColor = value;
RaisePropertyChanged(() => ReviewsColor);
}
}
public string ProductDescription
{
get
{
return _productDescription;
}
set
{
_productDescription = value;
RaisePropertyChanged(() => ProductDescription);
}
}
public bool ProductDescriptionIsVisible
{
get
{
return _productDescriptionIsVisible;
}
set
{
_productDescriptionIsVisible = value;
RaisePropertyChanged(() => ProductDescriptionIsVisible);
}
}
private bool _reviewIsVisible;
public bool ReviewIsVisible
{
get
{
return _reviewIsVisible;
}
set
{
_reviewIsVisible = value;
RaisePropertyChanged(() => ReviewIsVisible);
}
}
public Command MyCommand { protected set; get; }
public ICommand AddReviewCommand => new Command(async () =>
{
try
{
// await push.Instance.PopAllAsync();
await PopupNavigation.Instance.PushAsync(new ReviewPopUpView());
//await _navigation.PushAsync(new ReviewPopUpView());
}
catch (Exception ex)
{
}
});
public ICommand ShareCommand => new Command(async () =>
{
try
{
await CrossShare.Current.Share(new ShareMessage
{
Text = "Please download RcBazaar app from playstore",
Title = "Share Product",
Url = "http://rcbazaar.com/product.aspx?productid=6863"
});
}
catch (Exception ex)
{
}
});
private void OnRatingChanged()
{
try
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductsView, float>(this, "ProductsPage", (sender, arg) =>
{
if (arg != null)
{
Rating = arg;
}
});
}
catch (Exception ex)
{
}
}
private float _rating;
public float Rating
{
get
{
return _rating;
}
set
{
_rating = value;
RaisePropertyChanged(() => Rating);
}
}
public ICommand ViewImageCommand => new Command(async (parameter) => await ViewImage((string)parameter));
public async Task ViewImage(string ImageName)
{
try
{
await NavigationService.NavigateToAsync<ImageFullScreenViewModel>(ImageName);
}
catch (Exception ex)
{
}
}
public ICommand OverviewCommand => new Command(async () =>
{
try
{
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
}
catch (Exception ex)
{
}
});
public ICommand ReviewsCommand => new Command(async () =>
{
try
{
OverviewColor = "#DCDCDC";//lightgray
ReviewsColor = "#231f20";//blue color
ProductDescriptionIsVisible = false;
ReviewIsVisible = true;
}
catch (Exception ex)
{
}
});
}
}
Unsubscribing when home page disappears:
using FFImageLoading.Work;
using RCBazaar.CustomRenderer;
using RCBazaar.Helpers;
using RCBazaar.ViewModels;
using RCBazaar.Views.NavigationBarWithSideDrawer;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace RCBazaar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomeView : ContentPage
{
public HomeView()
{
try
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
//BindingContext = new HomeProductsViewModel(this.Navigation);
}
catch (Exception ex)
{
App.Current.MainPage.DisplayAlert(Constants.ConstantKeys.Alert, Constants.ConstantKeys.UIError, Constants.ConstantKeys.Ok);
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
}
}
c# android xaml xamarin xamarin.forms
I'm working on Xamarin Forms. Unsubscribe is not working in MessagingCenter when using between view models.
I'm trying to unsubscribing messagingCenter on OnDisappear() in ContentPage. The reason why I want to unsubscribing is because when I subscribing, it count as twice and the message also sent twice. It should not receive more than 1 time, same as subscribe too. Below my code.
BottomTabsViewModel where subscribing:
using RCBazaar.Base.ViewModels;
using RCBazaar.Services.Interfaces;
using RCBazaar.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace RCBazaar.ViewModels
{
public class BottomTabsViewModel : ViewModelBase
{
public ICommand HomeCommand => new Command(HomeTab_Clicked);
public ICommand CategoriesCommand => new Command(CategoriesTab_Clicked);
public ICommand CartCommand => new Command(CartTab_Clicked);
public ICommand ChatCommand => new Command(ChatTab_Clicked);
private string _badgeText;
private bool _isActiveHome;
public bool IsActiveHome
{
get
{
return _isActiveHome;
}
set
{
_isActiveHome = value;
RaisePropertyChanged(() => IsActiveHome);
}
}
private bool _isActiveCategory;
public bool IsActiveCategory
{
get
{
return _isActiveCategory;
}
set
{
_isActiveCategory = value;
RaisePropertyChanged(() => IsActiveCategory);
}
}
private bool _isActiveCart;
public bool IsActiveCart
{
get
{
return _isActiveCart;
}
set
{
_isActiveCart = value;
RaisePropertyChanged(() => IsActiveCart);
}
}
private bool _isActiveChat;
public bool IsActiveChat
{
get
{
return _isActiveChat;
}
set
{
_isActiveChat = value;
RaisePropertyChanged(() => IsActiveChat);
}
}
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public static string IsSelected { get; set; }
public BottomTabsViewModel()
{
BadgeCount();
if (IsSelected == "Home")
{
ActiveHome();
}
else if (IsSelected == "Category")
{
ActiveCategory();
}
else if (IsSelected == "Cart")
{
ActiveCart();
}
else if (IsSelected == "Chat")
{
ActiveChat();
}
else
{
ActiveHome();
}
}
private void BadgeCount()
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount", (sender, arg) =>
{
try
{
if (arg != null)
{
BadgeText = arg;
}
}
catch (Exception ex)
{
}
});
}
public void HomeTab_Clicked()
{
IsSelected = "Home";
NavigationService.NavigateToAsync<HomeProductsViewModel>();
}
public void CategoriesTab_Clicked()
{
IsSelected = "Category";
NavigationService.NavigateToAsync<MainCategoriesListViewModel>();
}
public void CartTab_Clicked()
{
IsSelected = "Cart";
NavigationService.NavigateToAsync<AccordianViewModel>();
}
public void ChatTab_Clicked()
{
IsSelected = "Chat";
NavigationService.NavigateToAsync<RCBazaarInformationViewModel>("Contact us");
}
public void ActiveHome()
{
IsActiveHome = true;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCategory()
{
IsActiveHome = false;
IsActiveCategory = true;
IsActiveCart = false;
IsActiveChat = false;
}
public void ActiveCart()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = true;
IsActiveChat = false;
}
public void ActiveChat()
{
IsActiveHome = false;
IsActiveCategory = false;
IsActiveCart = false;
IsActiveChat = true;
}
}
}
** ProductDetailViewModel where sending message:**
using FFImageLoading.Forms;
using Plugin.Share;
using Plugin.Share.Abstractions;
using RCBazaar.Base.ViewModels;
using RCBazaar.Model.ShoppingCart;
using RCBazaar.Services.Interfaces;
using RCBazaar.Services.Products;
using RCBazaar.ViewModels;
using RCBazaar.Views;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
[assembly: Dependency(typeof(ProductDetailsViewModel))]
namespace RCBazaar.ViewModels
{
public class ProductDetailsViewModel : ViewModelBase
{
private Product _productDetails;
private string _productName;
private long _productStock;
private double _productPrice;
private string _productDescription;
private ObservableCollection<string> _productImage;
private string _productSKU;
private ObservableCollection<CartItem> _basketItems;
private Color _badgeColor;
private long _productId;
private string _overviewColor;
private string _reviewsColor;
private bool _productDescriptionIsVisible;
private long _productRating;
private string _badgeText;
public ProductDetailsViewModel()
{
try
{
BasketItems = new ObservableCollection<CartItem>();
BadgeText = "0";
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
// _navigation = navigation;
var images = new List<string>() { "CourselStaticImage3.jpeg", "CourselStaticImage2.jpeg", "CourselStaticImage1.jpeg", "CourselStaticImage4.jpeg" };
ProductImage = new ObservableCollection<string>(images);
OnRatingChanged();
MyCommand = new Command(() =>
{
Debug.WriteLine("Position selected.");
});
int numbers = new int { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<WareHouse> wareHouses = new List<WareHouse>()
{
new WareHouse {WarehouseId=1, WareHouseName= "Banglore outlet", Items= numbers },
new WareHouse {WarehouseId=2, WareHouseName= "Pune outlet", Items= numbers },
new WareHouse {WarehouseId=3, WareHouseName= "Mumbai outlet", Items= numbers }
};
ListHeight = wareHouses.Count * 30;
WareHouses = new ObservableCollection<WareHouse>(wareHouses);
Reviews = new ObservableCollection<Review> { new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=2,IsReviewHelpful=true},
new Review { ReviewTitle="title",ReviewText="text",rating=4,IsReviewHelpful=false},
new Review { ReviewTitle="title",ReviewText="text",rating=0,IsReviewHelpful=true},
};
}
catch (Exception ex)
{
}
}
public class Review
{
public string id { get; set; }
public string ReviewTitle { get; set; }
public string ReviewText { get; set; }
public float rating { get; set; }
public string CreatedDate { get; set; }
public bool IsReviewHelpful { get; set; }
}
private ObservableCollection<Review> _reviews;
public ObservableCollection<Review> Reviews
{
get
{
return _reviews;
}
set
{
_reviews = value;
RaisePropertyChanged(() => Reviews);
}
}
public override async Task InitializeAsync(object navigationData)
{
_productDetails = (Product)navigationData;
ProductName = _productDetails.Name;
ProductStock = _productDetails.Qtyonhand;
ProductPrice = _productDetails.pricelist;
ProductDescription = _productDetails.FullDescription;
ProductRating = _productDetails.RatingSum;
// ProductImage = _productDetails.PictureBinary;
ProductId = _productDetails.ProductId;
ProductSKU = _productDetails.upc;
//// Update Basket/badgecount
//var basket = await _basketService.GetBasketAsync(userInfo.UserId, authToken);
//if (basket != null && basket.Items != null && basket.Items.Any())
//{
// BadgeCount = 0;
// BasketItems.Clear();
// foreach (var basketItem in basket.Items)
// {
// BadgeCount += basketItem.Quantity;
// await AddBasketItemAsync(basketItem);
// }
//}
}
//private async Task AddBasketItemAsync(BasketItem item)
//{
// BasketItems.Add(item);
// await ReCalculateTotalAsync();
//}
public ICommand ItemSelectedCommand => new Command(async () =>
{
try
{
WarehouseItemSelected = null;
}
catch (Exception ex)
{
}
});
public string BadgeText
{
get
{
return _badgeText;
}
set
{
_badgeText = value;
RaisePropertyChanged(() => BadgeText);
}
}
public ICommand AddtoCartCommand => new Command(async(e)=> await AddItemAsync(e));
private async Task AddItemAsync(object e)
{
try
{
var value = int.Parse(BadgeText);
if (value < 10)
{
BadgeText = (++value).ToString();
//BadgeColor = Color.FromHex("#000000");
//await Task.Delay(3000);
//BadgeColor = Color.FromHex("#FF9933");
}
else
BadgeText = "0";
MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
var selectedItem = (ProductDetailsViewModel)e;
await AddBasketItemAsync(selectedItem);
RaisePropertyChanged(() => BasketItems);
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
public Color BadgeColor
{
get
{
return _badgeColor;
}
set
{
_badgeColor = value;
RaisePropertyChanged(() => BadgeColor);
}
}
private async Task AddBasketItemAsync(ProductDetailsViewModel selectedItem)
{
try
{
// BasketItems.Add(selectedItem); this can be used if BasketItems is of type productdetauilsviewmodel
BasketItems.Add(new CartItem { BasketProductId = ProductId, WarehouseDetails = WareHouses });
await ReCalculateTotalAsync();
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("Alert!", "Something went wrong!..", "Ok");
}
}
private async Task ReCalculateTotalAsync()
{
//Total = 0;
//if (BasketItems == null)
//{
// return;
//}
//foreach (var orderItem in BasketItems)
//{
// Total += (orderItem.Quantity * orderItem.UnitPrice);
//}
//var authToken = _settingsService.AuthAccessToken;
//var userInfo = await _userService.GetUserInfoAsync(authToken);
//await _basketService.UpdateBasketAsync(new CustomerBasket
//{
// BuyerId = userInfo.UserId,
// Items = BasketItems.ToList()
//}, authToken);
}
//public ICommand AddtoCartCommand => new Command(async () =>
//{
// var value = int.Parse(BadgeText);
// if (value < 10)
// BadgeText = (++value).ToString();
// else
// BadgeText = "0";
// MessagingCenter.Send(this, "ShoppingCartCount", BadgeText);
//});
public ObservableCollection<CartItem> BasketItems
{
get { return _basketItems; }
set
{
_basketItems = value;
RaisePropertyChanged(() => BasketItems);
}
}
private int _listHeight;
public int ListHeight
{
get
{
return _listHeight;
}
set
{
_listHeight = value;
RaisePropertyChanged(() => ListHeight);
}
}
private WareHouse _warehouseItemSelected;
public WareHouse WarehouseItemSelected
{
get
{
return _warehouseItemSelected;
}
set
{
_warehouseItemSelected = value;
RaisePropertyChanged(() => WarehouseItemSelected);
}
}
public class WareHouse
{
public int WarehouseId{ get; set; }
public string WareHouseName { get; set; }
public int Items { get; set; }
public int quantity { get; set; }
}
private ObservableCollection<WareHouse> _wareHouses;
public ObservableCollection<WareHouse> WareHouses
{
get
{
return _wareHouses;
}
set
{
_wareHouses = value;
RaisePropertyChanged(() => WareHouses);
}
}
public long ProductRating
{
get
{
return _productRating;
}
set
{
_productRating = value;
RaisePropertyChanged(() => ProductRating);
}
}
public long ProductId
{
get
{
return _productId;
}
set
{
_productId = value;
RaisePropertyChanged(() => ProductId);
}
}
public ObservableCollection<string> ProductImage
{
get
{
return _productImage;
}
set
{
_productImage = value;
RaisePropertyChanged(() => ProductImage);
}
}
public string ProductName
{
get
{
return _productName;
}
set
{
_productName = value;
RaisePropertyChanged(() => ProductName);
}
}
public long ProductStock
{
get
{
return _productStock;
}
set
{
_productStock = value;
RaisePropertyChanged(() => ProductStock);
}
}
public double ProductPrice
{
get
{
return _productPrice;
}
set
{
_productPrice = value;
RaisePropertyChanged(() => ProductPrice);
}
}
public string ProductSKU
{
get
{
return _productSKU;
}
set
{
_productSKU = value;
RaisePropertyChanged(() => ProductSKU);
}
}
public string OverviewColor
{
get
{
return _overviewColor;
}
set
{
_overviewColor = value;
RaisePropertyChanged(() => OverviewColor);
}
}
public string ReviewsColor
{
get
{
return _reviewsColor;
}
set
{
_reviewsColor = value;
RaisePropertyChanged(() => ReviewsColor);
}
}
public string ProductDescription
{
get
{
return _productDescription;
}
set
{
_productDescription = value;
RaisePropertyChanged(() => ProductDescription);
}
}
public bool ProductDescriptionIsVisible
{
get
{
return _productDescriptionIsVisible;
}
set
{
_productDescriptionIsVisible = value;
RaisePropertyChanged(() => ProductDescriptionIsVisible);
}
}
private bool _reviewIsVisible;
public bool ReviewIsVisible
{
get
{
return _reviewIsVisible;
}
set
{
_reviewIsVisible = value;
RaisePropertyChanged(() => ReviewIsVisible);
}
}
public Command MyCommand { protected set; get; }
public ICommand AddReviewCommand => new Command(async () =>
{
try
{
// await push.Instance.PopAllAsync();
await PopupNavigation.Instance.PushAsync(new ReviewPopUpView());
//await _navigation.PushAsync(new ReviewPopUpView());
}
catch (Exception ex)
{
}
});
public ICommand ShareCommand => new Command(async () =>
{
try
{
await CrossShare.Current.Share(new ShareMessage
{
Text = "Please download RcBazaar app from playstore",
Title = "Share Product",
Url = "http://rcbazaar.com/product.aspx?productid=6863"
});
}
catch (Exception ex)
{
}
});
private void OnRatingChanged()
{
try
{
//Subscriber listen for the specific message. Here getting data for refresh the listView when add item to the database.
MessagingCenter.Subscribe<ProductsView, float>(this, "ProductsPage", (sender, arg) =>
{
if (arg != null)
{
Rating = arg;
}
});
}
catch (Exception ex)
{
}
}
private float _rating;
public float Rating
{
get
{
return _rating;
}
set
{
_rating = value;
RaisePropertyChanged(() => Rating);
}
}
public ICommand ViewImageCommand => new Command(async (parameter) => await ViewImage((string)parameter));
public async Task ViewImage(string ImageName)
{
try
{
await NavigationService.NavigateToAsync<ImageFullScreenViewModel>(ImageName);
}
catch (Exception ex)
{
}
}
public ICommand OverviewCommand => new Command(async () =>
{
try
{
OverviewColor = "#231f20";//blue color
ReviewsColor = "#DCDCDC";//lightgray
ProductDescriptionIsVisible = true;
ReviewIsVisible = false;
}
catch (Exception ex)
{
}
});
public ICommand ReviewsCommand => new Command(async () =>
{
try
{
OverviewColor = "#DCDCDC";//lightgray
ReviewsColor = "#231f20";//blue color
ProductDescriptionIsVisible = false;
ReviewIsVisible = true;
}
catch (Exception ex)
{
}
});
}
}
Unsubscribing when home page disappears:
using FFImageLoading.Work;
using RCBazaar.CustomRenderer;
using RCBazaar.Helpers;
using RCBazaar.ViewModels;
using RCBazaar.Views.NavigationBarWithSideDrawer;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace RCBazaar.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomeView : ContentPage
{
public HomeView()
{
try
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
//BindingContext = new HomeProductsViewModel(this.Navigation);
}
catch (Exception ex)
{
App.Current.MainPage.DisplayAlert(Constants.ConstantKeys.Alert, Constants.ConstantKeys.UIError, Constants.ConstantKeys.Ok);
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
}
}
c# android xaml xamarin xamarin.forms
c# android xaml xamarin xamarin.forms
edited Nov 20 '18 at 12:04
asked Nov 20 '18 at 10:41
praveena H M
226
226
When do you subscribe?
– EvZ
Nov 20 '18 at 10:56
Its subscribing In viewModel. There is a bottom view in a page. ControlTemplate through showing the view and that view has view model. There it is subscribing.
– praveena H M
Nov 20 '18 at 11:33
I would recommend to identify the problem that is causing multiple subscriptions at first place, however without seeing the code is a bit hard.
– EvZ
Nov 20 '18 at 11:37
yeah i wil put the classes right now.Means i will update my question.Please wait.
– praveena H M
Nov 20 '18 at 11:58
add a comment |
When do you subscribe?
– EvZ
Nov 20 '18 at 10:56
Its subscribing In viewModel. There is a bottom view in a page. ControlTemplate through showing the view and that view has view model. There it is subscribing.
– praveena H M
Nov 20 '18 at 11:33
I would recommend to identify the problem that is causing multiple subscriptions at first place, however without seeing the code is a bit hard.
– EvZ
Nov 20 '18 at 11:37
yeah i wil put the classes right now.Means i will update my question.Please wait.
– praveena H M
Nov 20 '18 at 11:58
When do you subscribe?
– EvZ
Nov 20 '18 at 10:56
When do you subscribe?
– EvZ
Nov 20 '18 at 10:56
Its subscribing In viewModel. There is a bottom view in a page. ControlTemplate through showing the view and that view has view model. There it is subscribing.
– praveena H M
Nov 20 '18 at 11:33
Its subscribing In viewModel. There is a bottom view in a page. ControlTemplate through showing the view and that view has view model. There it is subscribing.
– praveena H M
Nov 20 '18 at 11:33
I would recommend to identify the problem that is causing multiple subscriptions at first place, however without seeing the code is a bit hard.
– EvZ
Nov 20 '18 at 11:37
I would recommend to identify the problem that is causing multiple subscriptions at first place, however without seeing the code is a bit hard.
– EvZ
Nov 20 '18 at 11:37
yeah i wil put the classes right now.Means i will update my question.Please wait.
– praveena H M
Nov 20 '18 at 11:58
yeah i wil put the classes right now.Means i will update my question.Please wait.
– praveena H M
Nov 20 '18 at 11:58
add a comment |
2 Answers
2
active
oldest
votes
Few suggestions based on the code you shared:
- Subscribe on
OnAppearingand unsubscribe onOnDisappearing. Currently you are subscribing in the constructor.
MessagingCenter.Subscribefirst argument is a reference to an object that is going to listen to messages, if you want toMessagingCenter.Unsubscribeyou should use the same reference. The un-subscription is not working for you since you subscribe with a view model and unsubscribe with the page.
More information about MessagingCenter can be found in the official documentation.
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
add a comment |
You are subscribing to the MessagingCenter event you want in your ViewModel, but you are unsubscribing in your view. When a user calls MessagingCenter.Subscribe a user passes the instance of the class they are wanting the callback to come to.
Since you are calling unsubscribe in your view and passing your Views instance to unsubscribe, the ViewModel never gets unsubscribed which would result in an extra event call when you would navigate back to this page.
I would recommend instead of calling MessagingCenter.Unsubscribe in your View, call a method on your ViewModel to unsubscribe itself from MessagingCenter.
HomeView.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
ViewModel.Disappearing(); // Method to unsubscribe from MessagingCenter in ViewModel
}
HomeProductsViewModel.cs
public void Disappearing()
{
// Unsubscribe from events here because this is the class where you register them
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is toSubscribeandUnsubscribein the same class.
– TaylorD
Nov 20 '18 at 15:03
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391206%2fmessagingcenter-unsubscribe-not-working-in-xamarin-forms-when-using-between-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Few suggestions based on the code you shared:
- Subscribe on
OnAppearingand unsubscribe onOnDisappearing. Currently you are subscribing in the constructor.
MessagingCenter.Subscribefirst argument is a reference to an object that is going to listen to messages, if you want toMessagingCenter.Unsubscribeyou should use the same reference. The un-subscription is not working for you since you subscribe with a view model and unsubscribe with the page.
More information about MessagingCenter can be found in the official documentation.
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
add a comment |
Few suggestions based on the code you shared:
- Subscribe on
OnAppearingand unsubscribe onOnDisappearing. Currently you are subscribing in the constructor.
MessagingCenter.Subscribefirst argument is a reference to an object that is going to listen to messages, if you want toMessagingCenter.Unsubscribeyou should use the same reference. The un-subscription is not working for you since you subscribe with a view model and unsubscribe with the page.
More information about MessagingCenter can be found in the official documentation.
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
add a comment |
Few suggestions based on the code you shared:
- Subscribe on
OnAppearingand unsubscribe onOnDisappearing. Currently you are subscribing in the constructor.
MessagingCenter.Subscribefirst argument is a reference to an object that is going to listen to messages, if you want toMessagingCenter.Unsubscribeyou should use the same reference. The un-subscription is not working for you since you subscribe with a view model and unsubscribe with the page.
More information about MessagingCenter can be found in the official documentation.
Few suggestions based on the code you shared:
- Subscribe on
OnAppearingand unsubscribe onOnDisappearing. Currently you are subscribing in the constructor.
MessagingCenter.Subscribefirst argument is a reference to an object that is going to listen to messages, if you want toMessagingCenter.Unsubscribeyou should use the same reference. The un-subscription is not working for you since you subscribe with a view model and unsubscribe with the page.
More information about MessagingCenter can be found in the official documentation.
answered Nov 20 '18 at 14:37
EvZ
7,67442153
7,67442153
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
add a comment |
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
Thanks for the reply, I will go through this and if i get to know i will let you know.
– praveena H M
Nov 20 '18 at 15:01
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
For unsubscribe messagingCenter we should use the page or class instance. Which class through sending messages we should use that class instance for subscribe and unsubscribe. Thank you EvZ and TaylorD. Your solution through got the idea.
– praveena H M
Nov 27 '18 at 13:04
add a comment |
You are subscribing to the MessagingCenter event you want in your ViewModel, but you are unsubscribing in your view. When a user calls MessagingCenter.Subscribe a user passes the instance of the class they are wanting the callback to come to.
Since you are calling unsubscribe in your view and passing your Views instance to unsubscribe, the ViewModel never gets unsubscribed which would result in an extra event call when you would navigate back to this page.
I would recommend instead of calling MessagingCenter.Unsubscribe in your View, call a method on your ViewModel to unsubscribe itself from MessagingCenter.
HomeView.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
ViewModel.Disappearing(); // Method to unsubscribe from MessagingCenter in ViewModel
}
HomeProductsViewModel.cs
public void Disappearing()
{
// Unsubscribe from events here because this is the class where you register them
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is toSubscribeandUnsubscribein the same class.
– TaylorD
Nov 20 '18 at 15:03
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
add a comment |
You are subscribing to the MessagingCenter event you want in your ViewModel, but you are unsubscribing in your view. When a user calls MessagingCenter.Subscribe a user passes the instance of the class they are wanting the callback to come to.
Since you are calling unsubscribe in your view and passing your Views instance to unsubscribe, the ViewModel never gets unsubscribed which would result in an extra event call when you would navigate back to this page.
I would recommend instead of calling MessagingCenter.Unsubscribe in your View, call a method on your ViewModel to unsubscribe itself from MessagingCenter.
HomeView.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
ViewModel.Disappearing(); // Method to unsubscribe from MessagingCenter in ViewModel
}
HomeProductsViewModel.cs
public void Disappearing()
{
// Unsubscribe from events here because this is the class where you register them
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is toSubscribeandUnsubscribein the same class.
– TaylorD
Nov 20 '18 at 15:03
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
add a comment |
You are subscribing to the MessagingCenter event you want in your ViewModel, but you are unsubscribing in your view. When a user calls MessagingCenter.Subscribe a user passes the instance of the class they are wanting the callback to come to.
Since you are calling unsubscribe in your view and passing your Views instance to unsubscribe, the ViewModel never gets unsubscribed which would result in an extra event call when you would navigate back to this page.
I would recommend instead of calling MessagingCenter.Unsubscribe in your View, call a method on your ViewModel to unsubscribe itself from MessagingCenter.
HomeView.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
ViewModel.Disappearing(); // Method to unsubscribe from MessagingCenter in ViewModel
}
HomeProductsViewModel.cs
public void Disappearing()
{
// Unsubscribe from events here because this is the class where you register them
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
You are subscribing to the MessagingCenter event you want in your ViewModel, but you are unsubscribing in your view. When a user calls MessagingCenter.Subscribe a user passes the instance of the class they are wanting the callback to come to.
Since you are calling unsubscribe in your view and passing your Views instance to unsubscribe, the ViewModel never gets unsubscribed which would result in an extra event call when you would navigate back to this page.
I would recommend instead of calling MessagingCenter.Unsubscribe in your View, call a method on your ViewModel to unsubscribe itself from MessagingCenter.
HomeView.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
ViewModel.Disappearing(); // Method to unsubscribe from MessagingCenter in ViewModel
}
HomeProductsViewModel.cs
public void Disappearing()
{
// Unsubscribe from events here because this is the class where you register them
MessagingCenter.Unsubscribe<SortingOptionsViewModel>(this, "UpdateHomeList");
MessagingCenter.Unsubscribe<ProductDetailsViewModel, string>(this, "ShoppingCartCount");
}
answered Nov 20 '18 at 14:35
TaylorD
32016
32016
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is toSubscribeandUnsubscribein the same class.
– TaylorD
Nov 20 '18 at 15:03
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
add a comment |
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is toSubscribeandUnsubscribein the same class.
– TaylorD
Nov 20 '18 at 15:03
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
Ok will try and let you know.
– praveena H M
Nov 20 '18 at 14:48
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
TaylorD its not working. Can you tell me how do it through page instance .
– praveena H M
Nov 20 '18 at 14:55
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is to
Subscribe and Unsubscribe in the same class.– TaylorD
Nov 20 '18 at 15:03
If that didn't work I would suggest trying @EvZ solution. The main idea for both of our solutions is to
Subscribe and Unsubscribe in the same class.– TaylorD
Nov 20 '18 at 15:03
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
Ok i will try to use messagingcenter in same class for subscribe and unsubscribe. Thanks for all your support.
– praveena H M
Nov 21 '18 at 10:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391206%2fmessagingcenter-unsubscribe-not-working-in-xamarin-forms-when-using-between-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When do you subscribe?
– EvZ
Nov 20 '18 at 10:56
Its subscribing In viewModel. There is a bottom view in a page. ControlTemplate through showing the view and that view has view model. There it is subscribing.
– praveena H M
Nov 20 '18 at 11:33
I would recommend to identify the problem that is causing multiple subscriptions at first place, however without seeing the code is a bit hard.
– EvZ
Nov 20 '18 at 11:37
yeah i wil put the classes right now.Means i will update my question.Please wait.
– praveena H M
Nov 20 '18 at 11:58