Suppose this data model based on realm.io :
public class JournalEntry : RealmObject {
...
}
public class JournalOwner : RealmObject {
...
public RealmList<JournalEntry> Entries { get; }
}
I have a JournalEntriesPage , which show a ListView of all JournalEntry items of a JournalOwner .
Inside the corresponding view model, I pass the JournalOwner to the constructor and assign the JournalOwner.Entries to the property Entries .
public class JournalEntriesViewModel {
private Realm _realm;
private JournalOwner _owner;
public RealmList<JournalEntry> Entries {
get { return _owner.Entries; }
}
....
public JournalEntriesViewModel( JournalOwner owner ) {
_realm = Realm.GetInstance();
_owner = owner;
AddEntryCommand = new Command(AddEntry);
DeleteEntryCommand = new Command<JournalEntry>(DeleteEntry);
}
If I add a JournalEntry via the UI, it gets created but does not appear in the ListView .
If I move up the Navigation (to JournalOwner ) and then down to the entries of the owner, the ListView shows the newly created JournalEntry .
Somehow, the ListView of JournalEntry s does not know about the newly created JournalEntry.
The JournalEntriesPage binds the ListView like so:
<ListView
ItemsSource="{Binding Entries}"
x:Name="EntriesListView"
ItemTapped="OnItemTapped"
ItemSelected="OnItemSelected" >
How Do I mange the ListView show newly created entries immediately?
I feel, that I somehow need to inform observers (?) of Entries that its value changed.
In a corresponding situation, changes to the entries of
Owners = _realm.All<JournalOwner>();
do force the corresponding ListView to change immediately.
RealmList<T>
does not implement INotifyCollectionChanged
as @Jason has pointed out and so it cannot notify the ListView of changes. Your best bet would be to manually reload the data in the ListView.
RealmResults<T>
on the other hand does implement INotifyCollectionChanged
(though the API around that will soon change slightly ). That's why a ListView bound to _realm.All<JournalOwner>()
updates according to changes in the database.
The underlying infrastructure for observable lists is there , it's just a matter of time before it's exposed at the C#-level.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.