Making sure OnPropertyChanged() is called on UI thread in MVVM WPF app

前端 未结 5 2078
执念已碎
执念已碎 2020-11-28 07:15

In a WPF app that I\'m writing using the MVVM pattern, I have a background process that doing it\'s thing, but need to get status updates from it out to the UI.

I\'m

5条回答
  •  北海茫月
    2020-11-28 07:53

    I handle the BackgroundWorker.ReportProgress event outside of my ViewModel and pass the actual BackgroundWorker instance and the ViewModel into my class which defines the async method(s).

    The async method then calls bgWorker.ReportProgress and passes a class which wraps a delegate as the UserState (as object). The delegate I write as an anonymous method.

    In the event handler, I cast it from object back to the wrapper type and then invoke the delegate inside.

    All this means that I can code UI changes directly from the code that's running asynchronously, but it just has this wrapping around it.

    This explains it in more detail:

    http://lukepuplett.blogspot.com/2009/05/updating-ui-from-asynchronous-ops.html

提交回复
热议问题