Reputation: 113
I am trying to use the messaging center to send a simple string through the messaging center. And it works. But for some reason, despit only sending one message, it's caught twice.
Here's the sending page:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<App, string>(this, "Barcode", (sender, arg) => {
Device.BeginInvokeOnMainThread(() =>
{
HandleReadLabel(arg);
});
});
viewModel.LoadItemsCommand.Execute(null);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<App, string>(this, "Barcode");
}
public async Task<bool> HandleReadLabel(string labelText)
{
await Navigation.PopAsync();
MessagingCenter.Send<App, string>((App)Application.Current, "Trolley", labelText);
return true;
}
private async void btnTest_Clicked(object sender, EventArgs e)
{
string LabelTrolley = (sender as Button).Text;
await Navigation.PopAsync();
MessagingCenter.Send<App, string>((App)Application.Current, "Trolley", LabelTrolley);
}
two ways of sending that message: click on a button, or use the barcode scanner that is linked to the device (I have no problem with that subsciption, but I compared the two and couldn't find the issue)
Then the receiving page:
public Inv2Page(bool isMoveGoods)
{
InitializeComponent();
if (isMoveGoods)
viewModel = new Inv2ViewModel(ItemsListView, ItemsListViewTransfer, SelectionBinBoxView, "Move Goods");
else
viewModel = new Inv2ViewModel();
viewModel.LabelBin = "SCAN";
viewModel.IsMoveGoods = isMoveGoods;
BindingContext = viewModel;
MessagingCenter.Unsubscribe<App, string>(this, "Trolley");
MessagingCenter.Subscribe<App, string>(this, "Trolley", (sender, arg) => {
Device.BeginInvokeOnMainThread(() =>
{
HandleReadLabel(arg);
viewModel.LoadItemsCommand.Execute(null);
});
});
}
With no unsubsciption on disappearing.
The subscribed page is always behind the subsciption, and when I get the answer I want from the subscription, it sends the message back and disappear. Then it triggers the result twice.
Upvotes: 0
Views: 137