I'm following Gerald Versluis's videos https://www.youtube.com/watch?v=uxqQqyuZ3Qo https://www.youtube.com/watch?v=8_cqUvriwM8
All good (and great teaching), but uses code behind and I'm trying to be a good boy and learn mvvm and binding - and wishing I perhaps hadn't ;)
So I can get the data from my database and convert to a List and then cycle through the list to add to an observable collection which is referenced in the Xaml's CollectionView ItemsSource=.
This seems pretty longwinded and not "reactive" (is that the right word?)
So my question is how do I link the CollectionView in the Xaml directly to the SQLite database (through the Database.cs)?
I'm using CommunityToolkit.Mvvm.ComponentModel; and I know how to use [ObservableProperty] and now how to setup a class to be reactive - see .net maui Cannot get updated fields in an Observable collection to update in bound collection view
Do I apply these to the Classes.Friend.cs class and does sqlite understand all that?
Ok here's some code snippets MainPage.xaml
<CollectionView ItemsSource="{Binding FriendsOC}"
...
MainPage.xaml.cs --note the BindingContext here and not in xaml (refer .net Maui databinding to shell flyout item IsVisible property )
public MainPage()
{
InitializeComponent();
BindingContext = new CensusViewModel();
}
ViewModels.CensusViewModel.cs
public partial class CensusViewModel : ObservableObject
{
[ObservableProperty]
public ObservableCollection<Friend> friendsOC = new();
public CensusViewModel()
{
Console.WriteLine("heyupski G");
LoadData();
}
public async void LoadData() {
List<Friend> l = await App.Database.GetFriendsAsync();
foreach (Friend f in l)
{
FriendsOC.Add(f);
}
}
....
Database.Database.cs
public class Database
{
private readonly SQLiteAsyncConnection _database;
public Database(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Friend>();
_database.CreateTableAsync<FSGroup>();
}
public Task<List<Friend>> GetFriendsAsync()
{
return _database.Table<Friend>().ToListAsync();
}
public Task<int> SaveFriendAsync(Friend friend)
{
return _database.InsertAsync(friend);
}
public Task<int> DeleteFriendAsync(Friend friend)
{
return _database.DeleteAsync(friend);
}
public Task<int> UpdateFriendAsync(Friend friend)
{
return _database.UpdateAsync(friend);
}
public Task<int> DeleteAllFriendsAsync() => _database.DeleteAllAsync<Friend>();
...
}
Classes.Friend.cs
public class Friend
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string LName { get; set; }
public string FName { get; set; }
I think that's it.
Yet again I also ask the question: is there a definitive tutorial/explanation on binding and mvvm, because this is like climbing a slippery pole and I'm just hacking...