Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added db objects are not shown at the Winui3 ItemsControl loading completed. #3659

Closed
daniDevKr opened this issue Aug 13, 2024 · 7 comments
Closed

Comments

@daniDevKr
Copy link

What happened?

Added db objects are not shown at the Winui3 ItemsControl loading completed.

Repro steps

  1. At the ItemsControl control loading sample data are loaded into db.
  2. The app window not showing added Realm objects.

Version

net8.0-windows10.0.19041.0

What Atlas Services are you using?

Local Database only

What type of application is this?

Other

Client OS and version

Windows 11 Pro 23H2, build 22631.3958

Code snippets

Sample code repo link: Link

Stacktrace of the exception/crash you're getting

No response

Relevant log output

No response

Copy link

sync-by-unito bot commented Aug 13, 2024

➤ PM Bot commented:

Jira ticket: RNET-1173

@uixiu
Copy link

uixiu commented Aug 17, 2024

I didn't test your linked project.
From reading the MainWindow.xaml and MainWindow.xaml.cs, i think you were binding ItemsControl's ItemsSource to an empty Users[ObservableCollection] , instead of a public/internal _users[IQueryable] getter.

You can bind the ItemsControl's ItemsSource directly to...

public IQueryable Users => _users;

...to leverage collection change notification just like ObservableCollection.

[Sent from mobile app]

@daniDevKr
Copy link
Author

Yes, i fix it. But the problem already exist.

@uixiu
Copy link

uixiu commented Aug 19, 2024

I tested your updated project
Indeed, ItemsControl does not support collection change notifications from Realm's RealmResults collection.
I changed ItemsControl to ItemsRepeater and it correctly reacts to collection change notifications, including Add and Delete.

Here is the updated XAML

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="Winui3ItemsControlBug.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Winui3ItemsControlBug"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Padding="50">
        <TextBlock> Items:</TextBlock>
        <ItemsRepeater x:Name="ItemsControl1" ItemsSource="{x:Bind _users}" Loaded="ItemsControl1_Loaded">
            <ItemsRepeater.ItemTemplate>
                <DataTemplate x:DataType="local:User">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{x:Bind Firstname}" Padding="0 0 10 0"/>
                        <TextBlock Text="{x:Bind LastName}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsRepeater.ItemTemplate>
        </ItemsRepeater>
    </StackPanel>
</Window>

@uixiu
Copy link

uixiu commented Aug 21, 2024

Hi @daniDevKr ,
I believe this issue has been resolved, as it's not realm-dotnet related. Could you please close this issue if everything looks good?
Thank you!

@daniDevKr
Copy link
Author

I replaced IQueryable<User> with IRealmCollection<User> (on repo) because that supports changes notification, by INotifyCollectionChanged event implementation, but not data was showed.
The event IRealmCollection<User>.CollectionChanged was never sended.

@uixiu
Copy link

uixiu commented Aug 21, 2024

I replaced IQueryable with IRealmCollection (on repo) because that supports changes notification, by INotifyCollectionChanged event implementation, but not data was showed.

In your updated project, you used _users = _db.All<User>().AsRealmCollection();
In fact, you do not need this cast
In the following code doesImplement will be true, which indicates _db.All<User>() impalements IRealmColleciton<User>

private IQueryable<User> _users;
...
public MainWindow()
{
  ...
  bool  doesImplement = _users is IRealmCollection<User>;
  ...
}

The event IRealmCollection.CollectionChanged was never sended.

Try with the following code (taken from https://www.mongodb.com/docs/atlas/device-sdks/sdk/dotnet/react-to-changes/#std-label-dotnet-collection-notifications) to make sure the collection change notifications actually fires ( It correctly fires on a few machines/configs of mine, except for a [v12.0.0-v12.3.0] when compiled under x86 on amd64 Windows machine. So make sure you compile your project against x64).

private IQueryable<User> _users;
...
var subscriptionToken = _users.SubscribeForNotifications((sender, changes) =>
{
    if (changes == null)
    {
      // This is the case when the notification is called
      // for the first time.
      // Populate tableview/listview with all the items
      // from `collection`
      return;
    }
 
    // Handle individual changes
    foreach (var i in changes.DeletedIndices)
    {
      // ... handle deletions ...
    }

    foreach (var i in changes.InsertedIndices)
    {
      // ... handle insertions ...
    }
 
    foreach (var i in changes.NewModifiedIndices)
    {
      // ... handle modifications ...
    }
 
    if (changes.IsCleared)
    {
      // A special case if the collection has been cleared:
      // i.e., all items have been deleted by calling
      // the Clear() method.
    }
    });

Actually the notifications are triggered, which indicate it's not realm-dotnet issue but ItmesControl issue. As I suggested earlier, you can use ItemsRepeater wich will react to collection change (except perhaps range db transactions)

Not all list based controls behaves the same in WinUI. None play correctly with RealmCollection change notification in WPF. You can actually work around this by using the mentionned SubscribeForNotifications to update an auxiliary/proxy ObservableCollection, if that's suiting your needs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants