View not Updating in UWP, but in Android and iOS

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; }
}

}

Hi @UltimateWidder,

Your view seems to lack code. Could you show us the whole view? We need to see the source used for the CollectionView and how you bind to the command.
For formatting the code in a more readable manner, you can use 3 backticks to open a multiline code section and again 3 backticks to close the multiline code section. You can also specify the syntax highlighting by using the csharp tag or the xml tag right after the opening backticks, then new line. It’d look like the following but without the comment slashes

//```csharp
     var i = 5;
//```

Hello @ Andrea_Catalini,

thank you for the fast response. The formatting destroyed the copied code[…].
Hopefully this time it is shown completly:

```xaml
 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="RealmLiveUpdateTest.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <StackLayout>
        <CollectionView HeightRequest="300" ItemsSource="{Binding List}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Label
                            BackgroundColor="Red"
                            Text="{Binding Name}"
                            TextColor="White" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
        <Button Command="{Binding AddCommand}" Text="Add" />
    </StackLayout>
</ContentPage>


    using Xamarin.Forms;

namespace RealmLiveUpdateTest
{
    public partial class MainPage : ContentPage
    {
        MainPageViewModel context;
        public MainPage()
        {
            InitializeComponent();
            BindingContext = new MainPageViewModel();
            context = BindingContext as MainPageViewModel;
        }
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            context.Dispose();
        }
    }
}

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());

        }
    }
}

using Realms;
using System;

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; }
    }
}

The Binding of the command is functioniong well, the problem is the Binding of the ItemsSource of the CollectionView. While the List is binding correctly on Android and iOS, in UWP it is also bound, but the View is not updating when I add another Test object to the realm. It seems like the INotifyPropertyChanged is not invoking on UWP, but in Android and iOS it is.

Provided by the informations within the documentation https://www.mongodb.com/docs/realm/sdk/dotnet/fundamentals/object-models-and-schemas/
quoting “if you bind a ListView to a live query, then the list will update automatically when the results of the query change; you do not need to implement the INotifyPropertyChange interface.” it should also work on UWP or what am I missing?

Thank you for re-posting the code. We’re gonna take a look at this next week. We’ll keep you updated.

Thank you for your support, looking forward for the solution. I wish also a nice weekend.

Unfortunately I was sick the whole week. I hope to find time this week. But I can’t make promises.

Hello Andrea,
thank you for the update. I wish a good healing and bless you. :smiley:

I haven’t forgotten about this. I’m just waiting to have the time to check this. I’ll update you when I’ve tested the code.

Thank you Andrea for the informations. Looking for the result ;).

Hi @UltimateWidder,

Sorry for the long wait. I have finally had time to take a look at the issue you reported.
There’s nothing wrong with your code. I can indeed reproduce the problem that you are seeing. Unfortunately this is a known issue that hasn’t had yet enough priority to be reviseted.
You can read about the issue here. The summary is that this known issue is generated by a mix of 2 non-realm bugs, one in UWP’s ListView and the another one in Xamarin.Forms Android.

If you really need this to work, a workaround would be to use a 3rd party library that implements their own collectionView that pleases UWP.

I hope this helps you.

Hello Andrea,
thank you very much for your response. Your response clearifies my thought. At least it shows me I have to change the component.
It would be nice that this information is also provided in the .net sdk documentation as it is important to know how Realm should be used with UWP.

Kind Regards

Thank you for the feedback. Our hope is that the Android bug has finally being resolved and we can fix the UWP problem without having Android not working.
You could consider marking the issue as resolved if the help your received did what you expected.

Andrea