IEventAggregator in View

Topics: Bootstrappers & IoC
Jan 2, 2012 at 11:56 AM

Hi

I am using Coding4fun progressoverlay control in my Windows Phone 7 app. This control has Hide and Show methods for displaying the progress bar.

So in order to show and hide the progress bar I need to write the code in the code behind , (I would be intrested if this can be done in ViewModel).

So I have subscribe to events in the view. My viewmodel publishes this event

But when I use IEventAggregator in the constructor of view , my app doesn't get loaded. I assume the dependency injection is failing.

 

Below is the code of constructor on MainPage (view

public MainPage(IEventAggregator events)
        {
            InitializeComponent();
            this.events = events;
        }

Can anyone help me to achieve this.

 

Regards

Coordinator
Jan 2, 2012 at 1:20 PM

DI doesn't work on views out of the box. I'm sure that someone here has come up with a nice solution for the progress indicator though. 

Dec 18, 2013 at 11:31 AM
Same problem...
Maybe just pump resolved view instance through IoC.BuildUp (after InitializeComponents()) ?
At least it helps implement property injection and dispense with service locator.
Dec 18, 2013 at 10:27 PM
in the Constructor of your view
public class SomeView(){
  IEventAggregator _events;
  public SomeViewConstructor(){

   Bootstrapper bootstrapper = Application.Current.Resources["Bootstrapper"] as Bootstrapper;

   _events = bootstrapper._container.GetInstance<IEventAggregator>();

   _events.Subscribe(this);   
 }
}
Dec 19, 2013 at 2:52 AM
2 mvermef:
For service locator approach i'm better will do so: IoC.Get<IEventAggregator>(null).
But my desire is do without it.
Dec 19, 2013 at 5:00 AM
Edited Dec 19, 2013 at 5:02 AM
CompositionBatch batch = new CompositionBatch();
batch.AddExportedValue<IEventAggregator>("Test", new EventAggregator());
IoC.Get<CompositionContainer>().Compose(batch);

IoC.Get<IEventAggregator>("Test").Subscribe(this);

how to Destroy or release these batch and IEventAggregator ,in Dispose()?
thanks!