1

I'm working on porting a large .NET Framework application to .NET Core. The application uses WPF for the UI layer and utilizes MVVM Light (MvvmLightLibsStd10) for implementing MVVM architecture.

One of the problems I'm currently facing is with cross-thread UI updates. In the Framework version I was using GalaSoft.MvvmLight.Platform library to get access to DispatcherHelper which contains an easy-to- use DispatcherHelper.CheckBeginInvokeOnUI() function to disptach property change notifications.

Now this GalaSoft.MvvmLight.Platform library is available in net45 folder in the NuGet package, but not in netstandard1.0 folder, which gives me a hint that this lib might be platform (Windows?)-specific and therefore not suitable for .net core project.

Is it advisable to use this lib? If not, what would be the proper way of raising property change notifications in a thread-safe manner?

Update: Just found that this is one of the requested features on github:

https://github.com/lbugnion/mvvmlight/pull/79

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Your question is too broad at best, opinion-based at worst. That said, for basic non-collection properties, you should not need any extra code at all, as WPF handles cross-thread notification for `INotifyPropertyChanged` implicitly. WPF also has built-in handling for collection-changed notifications, but this requires some initial setup in your code to opt-in to the feature. – Peter Duniho Mar 24 '20 at 17:05
  • @PeterDuniho: I have had mixed results in the past. Sometimes UI gets updated cleanly without any thread-handling; sometimes it throws cross-thread marshaling exception, especially when changing props in timer tick events. I thought it would be safe to always do it through `DispatchHelper`. – dotNET Mar 24 '20 at 17:29
  • *WPF also has built-in handling for collection-changed notifications, but this requires some initial setup in your code to opt-in to the feature.* Any links/hints in this direction? – dotNET Mar 24 '20 at 17:30
  • _"I thought it would be safe to always do it through DispatchHelper"_ -- yes, that's true. Assuming done correctly, there's never any harm in explicitly ensuring you're on the right thread, even if the framework would do it for you anyway. _"Any links/hints in this direction?"_ -- have you searched Stack Overflow for topics related to `INotifyCollectionChanged` in WPF? Anyway, you could start here: https://stackoverflow.com/questions/2091988/how-do-i-update-an-observablecollection-via-a-worker-thread – Peter Duniho Mar 24 '20 at 18:20
  • @PeterDuniho: Thanks. Jon's answer on that page appears very helfpul. Also, I later found that MVVM Light provides one version of `Platform` library for each platform including android, xamarin and .netfx. I'll see if I can avoid that lib altogether. – dotNET Mar 24 '20 at 18:32

0 Answers0