Update an ObservableCollection with a background worker in MVVM

后端 未结 3 670
谎友^
谎友^ 2020-12-10 14:13

Ok, I recently implemented a background worker to perform saving and loading of data.

However, getting this to work on a save command has proved difficult.

B

3条回答
  •  被撕碎了的回忆
    2020-12-10 15:01

    Where you've got code which adds the item to the observable collection (presumably in the view model), wrap that Add call in a Dispatcher.BeginInvoke call.

    Admittedly that means the view model needs to know about the dispatcher, which then becomes awkward to test... fortunately it's not too hard to introduce your own IDispatcher interface and use dependency injection in the normal way.

提交回复
热议问题