OnImportsSatisfied is never hit


  • Hi all,

    In my little demo app I cannot get the OnImportsSatisfied routine ativated. I must be stupid simple, but I have a blind spot for it at the moment.

    This is my View.xaml:

    <UserControl x:Class="SilverlightApplication65.View"
        d:DesignHeight="300" d:DesignWidth="400">
            <vm:ViewModel x:Key="MyViewModel"/>
            <TextBlock Text="{Binding Path=Message, Source={StaticResource MyViewModel}}"/>

    This is the ViewModel.vb

    Imports System.ComponentModel.Composition
    Imports Microsoft.Practices.Prism.Events
    Public Class ViewModel
        Implements INotifyPropertyChanged
        Implements IPartImportsSatisfiedNotification
        <Import()> Private Property Local_EventAggregator As IEventAggregator
        Private Local_Message As String
        Public Property Message As String
                Return Local_Message
            End Get
            Set(value As String)
                Local_Message = value
                RaiseEvent PropertyChanged(Message, Nothing)
            End Set
        End Property
        Public Sub New()
            Local_Message = "Hello from the ViewModel"
        End Sub
        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
        Public Sub OnImportsSatisfied() Implements IPartImportsSatisfiedNotification.OnImportsSatisfied
            MessageBox.Show("Imports done")
            Local_EventAggregator.GetEvent(Of CompositePresentationEvent(Of String)).Publish("Event")
        End Sub
    End Class

    I want the View to instantiate the ViewModel as I do it in the View, and not in codebehind because I miss the intellisense options for the Properties e.g. for the TextBlock. With this approach I am able to set the Text property using the properties toolbox.

    What do I do wrong in this approach?



    Saturday, December 24, 2011 7:45 AM

All replies

  • Hi Peter,

    According to your discription, I will suggest you to take a look at an example shown in below blog post: 

    Hope it helps.

    Best Regards,

    Sunday, December 25, 2011 9:54 PM
  • Hi Haixia Xie,

    Thanks for your reply. I'm afraid though, that it doesn't help me further. Examples by Dan Wahlin (also from the Firestarter event of last year and on Silverlight TV) have inspired me to choose the what he calls "declarative" method of binding the ViewModel.

    The ViewModel is bound allright, that's not the problem. The Message appears in the View. In my code you can see that I am able to Bind the TextBox.Text property to the ViewModel's Message property. That is all OK. It compiles and runs.

    The problem is, that the OnImportsSatisfied is never hit, and I do not understand why that is so. The result of this is that the importing of the IEventAggregator is never carried out. Ergo this (simple example) module cannot publish or subcribe to EventAggregators events.

    So it's not the Binding of the ViewModel that is the problem, in fact it is the failure to import the EventAggregator.

    If you have any suggestions for that, I'd be very pleased.

    Happy Holidays!



    Monday, December 26, 2011 5:26 AM
  • Hi Haixia Xie,

    Meanwhile I have found a solution for this. I have described it in another thread.

    (the links won't work, so you will find it here: Look for the post "Help Needed: MEF with MVVM and dependency injection" ) Scroll all the way down there.

    It appears that there is a difference in importing behaviour whether you instantiate a ViewModel from code-behind or from xaml code.

    Thank you!



    Tuesday, December 27, 2011 9:57 AM
  • Hi Haixia Xie,

    It seems I've found the reason for this behaviour: it appears that there is a difference when I instantiate the ViewModel from codebehind and when I do that from then View.xaml. In the case of instantiating from the View.xaml, I will have to tell the c'tor explicitly to do it's imports, by issuing the command "CompositionInitializer.SatisfyImports(Me)".

    I have this posted in another thread, where I was looking for the behaviour of MEF.

    Best regards,


    Wednesday, December 28, 2011 10:15 AM