简体   繁体   English

通过更改的RealmList <>更新ListView

[英]Updating ListView by changed RealmList<>

Suppose this data model based on realm.io : 假设此数据模型基于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 . 我有一个JournalEntriesPage ,它显示JournalOwner的所有JournalEntry项目的ListView

Inside the corresponding view model, I pass the JournalOwner to the constructor and assign the JournalOwner.Entries to the property Entries . 在相应的视图模型内,我将JournalOwner传递给构造函数,并将JournalOwner.Entries分配给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 . 如果我通过UI添加JournalEntry ,则会创建它,但不会出现在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 . 如果我向上移动导航(到JournalOwner ),然后向下移动到所有者的条目,则ListView将显示新创建的JournalEntry

Somehow, the ListView of JournalEntry s does not know about the newly created JournalEntry. 不知何故, JournalEntryListView不了解新创建的JournalEntry。

The JournalEntriesPage binds the ListView like so: JournalEntriesPage像这样绑定ListView:

<ListView 
   ItemsSource="{Binding Entries}" 
   x:Name="EntriesListView" 
   ItemTapped="OnItemTapped"     
   ItemSelected="OnItemSelected" >

How Do I mange the ListView show newly created entries immediately? 如何管理ListView立即显示新创建的条目?

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. 强制相应的ListView立即更改。

RealmList<T> does not implement INotifyCollectionChanged as @Jason has pointed out and so it cannot notify the ListView of changes. 正如@Jason指出的那样, RealmList<T>没有实现INotifyCollectionChanged ,因此它无法将更改通知给ListView。 Your best bet would be to manually reload the data in the ListView. 最好的选择是手动重新加载ListView中的数据。

RealmResults<T> on the other hand does implement INotifyCollectionChanged (though the API around that will soon change slightly ). RealmResults<T>确实实现了INotifyCollectionChanged (尽管周围的API 不久将略有变化 )。 That's why a ListView bound to _realm.All<JournalOwner>() updates according to changes in the database. 这就是绑定到_realm.All<JournalOwner>()的ListView根据数据库中的更改进行更新的原因。

The underlying infrastructure for observable lists is there , it's just a matter of time before it's exposed at the C#-level. 可观察列表的底层基础结构已经存在 ,在C#级别公开它只是时间问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM