IHandle not working on View's behind code

Feb 18, 2014 at 8:21 PM
Edited Feb 18, 2014 at 8:21 PM
I currently have 3 separate projects in a solution, Main application which contains my bootstrapper, a View project, and a ViewModel project

The main application's bootstrapper is set up correctly to tie the view and viewmodel together.

I need to be able to access the View's behind code from the viewmodel, so i set the View's class declaration as such

public class MyView: Window, IHandle<string>
public void Handle(string data) { }

However, when i Publish from my viewmodel, this never gets hit. If i setup the IHandle implementation in the bootstrapper class instead, then it does fire off there, but that does me no good, as i need to be at the view instance level in order to do the things i want to do.

in my bootstrapper, i am subscribing to the IEventAggregator correctly since IHandle IS working from the bootstrapper.

any idea what i'm doing wrong or am i going about access the behind code in the wrong way?
Feb 18, 2014 at 8:41 PM
You need to have the ViewModel subscribe to the IEventAggregator.

Feb 18, 2014 at 9:03 PM
Edited Feb 18, 2014 at 9:29 PM
Let me elaborate on the problem.

UtilityViewModel.cs (from my ViewModel project)
public class UtilityViewModel {
private IEventAggregator myevent;
        public UtilityViewModel(IWindowManager window, IEventAggregator events)
myevent = events;

        public void SendDataBackToBehindCode(string message)
            //this will get called
View.xaml.cs (From my View project

public partial class UtilityView : Window, IHandle<string>
  public void Handle(string data)
  // it never gets here
but again, if i put the public void Handle in my bootstrapper code, then THAT codes does get fired, so i know the "publish" is working.. just not from the View's behind code
Feb 18, 2014 at 9:25 PM
Hmm, looks correct. Have you place a breakpoint in the constructor to see you're getting the right values for window and events?

Feb 18, 2014 at 9:26 PM
And you should be using IoC.Get<IEventAggregator>() instead of passing those values. Thats why it's there.

Feb 18, 2014 at 9:46 PM
do i need to do any subscriing in the View's behind code or simply just that IHandle like i have it?

ive replaced those parametesr with just calling IoC.Get<IEventAggregator>(), but the Handle method is still not getting called.

i put a break point in my viewmodel to check to see if my event is returning null or not and its not null.

any ideas? is CM supposed to look at the View's behind code to begin with to see if its implementing IHandle?
Feb 18, 2014 at 9:48 PM
is it possible that the bootstrapper is "overriding" my View class for IHandle?

because like i said, if i implement IHandle<string> inside my bootstrapper class, it gets hit there.. theoretically if i have my bootstrapper AND my view both implement IHandle<string>, are they BOTH supposed to get called?
Feb 18, 2014 at 10:01 PM
I don't know what happens if you have behind code in the view. I typically delete it.

Feb 18, 2014 at 10:06 PM
ah okay i misunderstood. So you've never be able to use IHandle on a View's behind code before?

From my view model class, i am trying to raise an event so that my view's behind code can run some things related to storyboards which i have no access to in the viewmodel (and wouldn't want to since that would break MVVM).

for now, im just using an invisible textbox, ontargetupdate, binding my args to the textbox's Tag property, so that i can get to the View in order to access the storyboard object to do direct manipulation to it. I was hoping IHandle would work with behind code so that i wouldn't have to do this nasty work around..

guess IHandle allows me to pass parameters around to other viewmodels, but not to other view behind codes... how disappointing
Feb 18, 2014 at 10:14 PM
I think you've misunderstood me. I was only referring to your use of the Views, not the IEventAggregator. I've look at the code the IEventAggreator and it doesn't have anything specific to Views or ViewModels. I've actually copied the code out and used it in a WinForms app. The Subscribe function simply adds the object passed into a list. When a publish occurs, it iterates the entire list looking for objects that implement the IHandle interface that match the type being published. Very simple. Without a sample, it's hard to say why you aren't catching the expected publish.

Feb 18, 2014 at 11:14 PM
if thats all its doing then i have no idea why the view's behind code isnt being evaluated. do instances get removed automatically from the list? i will provide sample code in the next day or so when i have some time.
Feb 18, 2014 at 11:28 PM
ok got it working.

the problem was my bootstrapper had 2 instances of the IEventAggregator so i was pulling the wrong one when i was subscribing from the View's behind code.

thanks for your help though. cheers