Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Last active January 14, 2020 20:14
Show Gist options
  • Save jamesmontemagno/e002dc729cb7402a1390 to your computer and use it in GitHub Desktop.
Save jamesmontemagno/e002dc729cb7402a1390 to your computer and use it in GitHub Desktop.
Simple example of using the messaging center.
public ObservableCollection<TripExpense> Expenses { get; set; }
public ExpensesViewModel()
{
Expenses = new ObservableCollection<TripExpense>();
//Subscibe to insert expenses
MessagingCenter.Subscribe<TripExpense>(this, "AddNew", (expense) =>
{
Expenses.Add(expense);
});
//subscribe to update expenxes
MessagingCenter.Subscribe<TripExpense>(this, "Update", (expense) =>
{
ExecuteUpdateExpense(expense);
});
}
private async Task ExecuteSaveCommand()
{
if (IsBusy)
return;
IsBusy = true;
//Send a message to insert/update the expense to all subscribers
if(isNew)
{
MessagingCenter.Send(expense, "AddNew");
}
else
{
MessagingCenter.Send(expense, "Update");
}
IsBusy = false;
navigation.PopAsync();
}
@fkbeys
Copy link

fkbeys commented Jan 14, 2020

It works in page.cs side. but if i apply this in my view model, the list adds multiple items.
i dont know why it happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment