Hello,
Welcome to our Microsoft Q&A platform!
When you tried to delete the seleted item by the following code, you can also try to remove the item form the Contacts
. Since the type of Contacts
is ObservableCollection<Contact>
, once you delete the item from the Contacts
, the UI will updated automatically.So you can remove the seleted item after you delete in your database.
case "Delete":
using (SQLiteConnection sQLiteConnection = new SQLiteConnection(App.DatabaseLocation)) {
sQLiteConnection.CreateTable<Contact>();
int row = sQLiteConnection.Delete(SelectedItem);
if (row > 0) {
await mainPage.DisplayAlert("Deleted", "Element has been deleted successfully", "OK");
}
// delete the selected item in `Contacts `, then the UI will updated automatically.
}
break;
In addition, I don't understand why you call 'ContactsVM = new ContactsVM(); 'to create a ContactsVM object and call function Populatedatabase()
in method OnAppearing()
on page ContactsPage
. And I have found you have called function Populatedatabase();
in your ContactsVM
.
If I understand correctly, So you can set BindingContext
for your page and set the ItemsSource
for control to Contacts
:
public partial class MainViewXaml : ContentPage
{
public ContactsVM contactsVM { get; set; }
public MainViewXaml ()
{
InitializeComponent();
contactsVM = new ContactsVM ();
BindingContext = contactsVM ;
}
}
And in your ContactsPage.xaml
, set the ItemsSource
for your ListView to Contacts
<ListView x:Name="lstView" ItemsSource="{Binding Contacts }" >
</ListView>
Update 1:
You can refer to the following code in my demo:
In the new contact page, , we can send a message to the contact list page :
async void OnSaveClick(object sender, EventArgs e)
{
NewsarticleElements topicText = new NewsarticleElements();
topicText.topicTitle = TopicTitle.Text;
topicText.articleText = ArticleText.Text;
MessagingCenter.Send(this, "AddItem", topicText);
//await Navigation.PopAsync();
await Navigation.PopModalAsync();
}
And in the contact list page, we can Subscribe message as follows(my page is MainPage
):
public MainPage()
{
InitializeComponent();
//BindingContext = new MainPageViewModel();
MessagingCenter.Subscribe<NewPage, NewsarticleElements>(this, "AddItem", (obj, item) =>
{
var newItem = item as NewsarticleElements;
NewItem temp = new NewItem() { NewsName= newItem.topicTitle, ArticleText= newItem.articleText };
NewsItemList.Add(temp);
NewsView.ItemsSource = null;
NewsView.ItemsSource = NewsItemList;
});
}
Update 2
You can try the following code:
- In
ContactsVM.cs
, Subscribe message in the constructor of classContactsVM
:public ContactsVM() { Populatedatabase(); AddContact = new Command(async () => { var mainPage = Application.Current.MainPage; await mainPage.Navigation.PushAsync(new AddContact()); }); MessagingCenter.Subscribe<AddContactVM, Person>(this, "AddItem", (obj, item) => { Person newItem = item as Person; Contacts.Add(newItem); }); // other code }
- send message in the constructor of class
AddContactVM
:public AddContactVM() { person = new Person(); SaveCommand = new Command(async () => { await DBHelper.AddContact(person.Name, person.PhoneNumber, person.Address); // send message here MessagingCenter.Send<AddContactVM, Person>(this, "AddItem", person); var main = Application.Current.MainPage; await main.Navigation.PopAsync(); }); }
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.