wpf - have dll callback notify mainwindow - wpf

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.

Related

C# WPF Caliburn Micro Event Aggregation for services

I am fairly new to MVVM.
I find a lot of examples, where ViewModels can publish events to other VMs using event aggregations.
My question is: Can I use the event aggregation to publish an event from a model, service or helper class, and VMs are reacting to it?
Example:
Helper class using tapi3.dll to implement tapi features. I now want to sent the TAPI_EVENTS as events to VMs to react to incoming calls (e.g. display different Usercontrol)
Is this possible? Or do I break the MVVM pattern?
Thanks!

Custom control wpf, enabling invoking control methods to support MVVM pattern

I am writing a custom control in WPF, and i have a couple of methods in the control that the user of my control can invoke.
Although it can be invoked directly, due to requests from users, i want to enable the control's methods to be invoked in a MVVM manner.
After research, i take it i have to implement ICommandSource on my control.
The MSDN has an example but i find it too much over-engineered for my situation (or maybe because i don;t understand it at all!).
All i want is to let my consumers to call my method via a command rather than put their calling code in view's code behind.
Please advise with a simple sample code to implement this. Thanks.
In the Blend SDK is a behavior MethodInvoker (if my memory serves correct) that allows you to bind to a method. That way you do not have to implement ICommand.
If you have access to reflect over the .NET code (or you can download the .NET source code via download) look into how MS does it with ButtonBase. Yes, there is a lot of code under the hood, but that's because if you're going to create a custom control you should account for both scenarios of RoutedEvents and Commands.

Difference in Event Handling in Silverlight and WPF - Thread Affinity issues

I have been developing a Lync Silverlight application in Silverlight and now I am trying to shift it to WPF.
However, I am facing some thread affinity issues. For example I display the Lync client's state on my page in a textblock, and so in my code behind have wired a state changed event handler, that writes the new state into the textblock whenever the state of Lync client changes.
Now, this worked perfectly in silverlight but seemingly is not allowed in WPF.
Now my questions are:
How come it works in Silverlight bt not in WPF, even though Silverlight is supposed to be a subset of WPF?
Thread affinity is an important concept and I know we can use invoke dispatcher, but doesn't it just beat the concept of asynchronous programming in form of event handlers and callbacks?
I have a button defined in my XAML page, and the click event handler defined on it can access other UI elements, it does not suffer the problem outlined above.
But if I define a LyncClient instance in my code-behind, event handlers defined on it cannot access the UI elements. Why so, I detected no such difference between UIElements and other objects in Silverlight?
Based on above comments, I'll suggest the following "answer"...
I would guess it is more likely than not that there is some sort of different in the way that the SL API was written than that of the WPF api. That could explain the difference in the thread that is used when the API issues the callback. To verify this, you could:
Ask MS directly
Put some diagnostics code in your callback method to log the thread ID and compare that to the main thread of the application. Do this for both SL and WPF to see if they are the same or different threads.
Open the assemblies in Reflector to inspect how each API was written.
In terms of handling this specific situation, in your callback, you could:
Get the dispatcher object (different for SL than WPF) and always issue UI updates through Dispatcher.Invoke.
Use databinding and INotifyPropertyChanged to insulate the UI from the property. You could delcare a property on a ViewModel or in the code behind. Then bind the UI's textbox to that property. Databinding has some smarts in it that will automatically marshal property changes to the correct thread (in most cases anyway).
Hope that helps.

WPF interactionRequests in PRISM

I have what, on the face of it, seems to be a really simple requirement - to be able to show a messagebox from within the view model of my WPF prism application.
Reading the documentation everything sounds good when I'm reading about Interaction Requests but I then find out that WPF doesn't support PopupChildWindowAction.
How are people getting around this. Basically I want a Messagebox in my shell module / or a infrastructure module that will subscribe to events and popup when that event is published.
Another issue I had was I want the popup to be centered on the parent window (the shell).
Just wondered how other people approached this. There seem to be a number of different ways to go but neither seem to fit the bill exactly.
From A CodePlex post by Karl Shifflet:
I've written a WPF version of the Interaction Request for my the Box MVVM Training here:
http://visualstudiogallery.msdn.microsoft.com/en-us/3ab5f02f-0c54-453c-b437-8e8d57eb9942
Install this Visual Studio Extension.
Create a new project with the MVVM Training Template.
Check out DialogInteractionRequestView.xaml and its implementation.
Cheers,
Karl
Use the RegionPopupBehavior from Prism 2.2 RI.
Use the EventAggregator in PRISM to subscribe to events, and have an in-memory presenter that listens for an event and then creates a view using the event data and calls ShowDialog on the view.
The dialog result can then be used to publish a 'response' event that would be routed back to the process that initiated the event that resulted in the display of the dialog.
Since PopupChildWindowAction is only in Silverlight, I have created my own PopupAction by inheriting from TriggerAction class and simply overridden body of Invoke() method to bring up a PopupWindow where I can pass any UserControl from xaml within the prism interaction trigger tag. From within ViewModel I am raising interactivity request event which triggers my PopupAction in view and opens the popup with desired user control being displayed onto it. Seems to work. I'll need to polish the example more. But here is a link -
http://wpfgrid.blogspot.com/2013/01/simple-prism-mvvm-way-to-display-dialog.html#step3

Using MVVM Foundation Messenger to Show Dialog

I'm building a WPF app and trying to conform to MVVM best practices. I'm using the MVVM Foundation framework and noticed the Messenger class, which I've read should be used for handling dialogs in WPF. This sounds great, but I'm totally not understanding how to use a Messenger for this purpose. Literally, all I want to do is open a modal About dialog --I don't need to pass any messages back and forth.
Was the intent of the Messenger class to be used for cases where dialogs require a message from its parent, or return a message to its parent? Is it overkill for an About dialog? Would I be better off simply adding code to an event handler to show the dialog?
The idea behind the messaging pattern doesn't specifically have anything to do with showing dialogs. The idea is simply to provide a decoupled way to communicate between ViewModels.
You can leverage this infrastructure to solve your problem but you will have to implement the showing of the dialog yourself.
As Phillip showed above you can send messages between ViewModels. When your ViewModel receives the message it can set it's own internal property, say "ShowDialog", to true.
You can then have a binding that reacts to this property change operation and opens a dialog.
I have also built a simple messaging framework for the MVVM pattern that borrows from Josh's idea (and several other existing frameworks) you can read about it here
Say you have a parent view and a dialog view. In MVVM they would both have a view model. It is good to keep these view models decoupled, i.e. they don't have references to each other. And yet they need to communicate to each other. The Messenger class acts as a go between or Mediator to mediate the communication of information between the two classes. See the code taken from Josh's blog.
Here is Object A. It's call to the mediator's Register method implements: when I receive the message ObjectBSaidSomething, from the mediator, I'll cache it in the member WhatObjectBSays.
Here is Object B, which implements: I'm going to send a message ObjectBSaidSomething. Note, that Object B knows nothing about Object A. There might be nothing listening for ObjectBSaidSomething, or 100 objects listening for ObjectBSaidSomething, but Object B doesn't know and doesn't care. This is good decoupling, and this is why the Mediator pattern is a good idea. And this is the way the MVVM foundation is recommending that information is passed between view models.

Resources