Hello folks,
I am doing my first steps with Realm .Net SDK and found a probleme where I don’t see how to solve it:
I was examining howto use Realm .Net SDK in Xamarin and automatically update the view using the MVVM pattern.
While it is working with Android and iOS, it is not working on UWP.
Can anyone leave a comment on my example code what I am not implementing correctly or not seeing?
Kind regars
The Example Code starting with the View:
<?xml version="1.0" encoding="utf-8" ?>
<CollectionView.ItemTemplate>
</CollectionView.ItemTemplate>
the ViewModel:
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using Realms;
using CommunityToolkit.Mvvm.Input;
using System.Diagnostics;
using System.Linq;
namespace RealmLiveUpdateTest
{
public partial class MainPageViewModel : ObservableObject
{
public IEnumerable<Test> List { get; }
Realm realm;
public MainPageViewModel()
{
realm = Realm.GetInstance();
Test t1 = new Test("1");
Test t2 = new Test("2");
Test t3 = new Test("3");
Test t4 = new Test("4");
realm.Write(() =>
{
realm.RemoveAll<Test>();
realm.Add(t1);
realm.Add(t2);
realm.Add(t3);
realm.Add(t4);
});
List = realm.All<Test>();
}
internal void Dispose()
{
realm.Dispose();
}
[ICommand]
public void Add()
{
Test t5 = new Test("5");
try
{
realm.Write(() =>
{
realm.Add(t5);
});
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
Debug.WriteLine(realm.All<Test>().ToList().Count.ToString());
Debug.WriteLine(List.ToList().Count.ToString());
}
}
}
the model:
using System.Text;
namespace RealmLiveUpdateTest
{
public class Test : RealmObject
{
public Test()
{
}
public Test(string name):this()
{
Name = name;
}
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
}
}