Reputation: 21
in a UWP c# application, how do I send a message from one object to another? This without creating specific parameters or methods.
I'm thinking of using a message bus, but I did not find anything for UWP C# app.
In iOS applications I used this https://gist.github.com/hollance/3121457 while in Android applications, I used the LocalBroadcastManager.
Is there any native or not API that allow me to do the same thing in an UWP application?
Upvotes: 0
Views: 289
Reputation: 409
I was wrestling with this myself before I finally gave up and stopped trying to make UWP sane. Fair warning TLDR;
It is difficult and time consuming to successfully use a message bus communication model anywhere in a UWP application. The way it is done in other applications, when done in UWP will cause memory leaks and nasty exceptions that will take days to track down.
The reason for this is that if you use the built-in Navigation framework to manage your views, you are not in control of when those views (or even ViewModels) get created and released. So, what will happen when a View or ViewModel subscribes to a message from the service bus? That subscription will never be released. The next time a View or ViewModel is new'ed up, another subscription comes and the old one will still be there...along with the old View and ViewModel instance.
This is all because UWP never bothered to implement IDisposable in the framework, leaving a developer with nothing but an extremely convoluted way to execute any "cleanup" type code associated with the abandonment of a View or ViewModel. I get why the navigation framework needs to manage that. In general, with MVVM it is so incredibly easy to completely ruin good performance that every stop had to be pulled to eek out every performance improvement including a View caching strategy. What's the alternative? Abandon one of the key selling points of UWP, the Navigation system. Do you really want to write your own and/or manage that yourself?
Sort of, it can still be done. See this question for a reference of just how ceremonious it is to do the simplest of cleanup in a UWP application: UWP C# Cleaning up secondary page
You will find article and question upon article and question out there espousing the virtues of trying integrate counter-MVVM patterns into a UWP app to try and make it more non-MVVM friendly. It wasn't designed for that. Sure, you can do it, sort of, but I wouldn't go that way unless you want a constant nagging stream of issues throughout the entire lifecycle of the application.
If you can't put more sanity into UWP, you'll need accommodate the insanity in UWP. While I'm not a fan of MVVM patterns in general, UWP among the least desirable frameworks I've ever had the displeasure of making work.
Passing a message usually means passing data. In UWP, and MVVM in general, you don't do that. Instead, you come up with long life data models, update them directly, and then notify Views, ViewModels, and anything else that wants to know, about changes to the one authoritative source of that data in the application.
It's a VERY different way of thinking about data change propagation in an application and it can be a gigantic time wasting PITA, but there it is.
Oh, and if you have any background tasks, network enabled applications with server side push, or anything else updating those long life data models other than the controls in the Views running on the main thread, then multiply that gigantic time wasting PITA by a factor of at least 3.
Upvotes: 0
Reputation: 1889
Appservices should make that possible: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service
Upvotes: -1