Last active
January 14, 2020 20:14
-
-
Save jamesmontemagno/e002dc729cb7402a1390 to your computer and use it in GitHub Desktop.
Simple example of using the messaging center.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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