I have a 3rd party dll that I am including in a C# WPF project with Dllimport directives.
I have a static c# function that they call as a callback when a certain hardware event occurs.
I would like to accomplish what an old school PostMessage would accomplish. Just notify my mainwindow that the callback occurred. I know I could just get my window and cast and call the mainwindow’s function directly but that seems a little flaky. I like the old async PostMessage pattern. Perhaps my question is more about loosely coupling a couple of components in the app.
Should I just invoke a command?
You should look at using loosely coupled events as you suggest. Some useful starting points are
Prism’s Event Aggregator
Caliburns’s Event Aggregator
MVVM Light‘s Messenger
which all do similar pub-sub messaging.
I suspect your callback will be on another thread so you’ll probably need to use Dispatcher.Invoke to marshal the call to your UI thread.