Hi,
I'm new to MEF and would like to do a software where the host needs to communicate properties to each modules. I created a Small project as a test. A HostApp, an Interface and a PlugIn. The HostApp shows the PlugIn and a Button. When I click on the button, it send a list of string to the PlugIn. The PlugIn contains a listbox and I want that ListBox to be refreshed and show the list of string. The problem I have, is that after sending the list to the PlugIn, the properties contains all the strings, but the ListBox do not update.
Interface
Namespace API Public Interface IPlugIn Property Files As System.Collections.ObjectModel.ObservableCollection(Of String) ReadOnly Property PlugInUI As System.Windows.Controls.UserControl End Interface End Namespace
PlugIn
Imports System.ComponentModel.Composition <Export(GetType(Contract.API.IPlugIn))> Public Class UserControl1 Implements Contract.API.IPlugIn, System.ComponentModel.INotifyPropertyChanged, System.Collections.Specialized.INotifyCollectionChanged Private _files As System.Collections.ObjectModel.ObservableCollection(Of String)
Public Property Files As System.Collections.ObjectModel.ObservableCollection(Of String) Implements Contract.API.IPlugIn.Files
Get If IsNothing(_files) Then _files = New System.Collections.ObjectModel.ObservableCollection(Of String) End If Return _files End Get Set(value As System.Collections.ObjectModel.ObservableCollection(Of String)) _files = value OnPropertyChanged("Files")
OnCollectionChanged(New Collections.Specialized.NotifyCollectionChangedEventArgs _
(Specialized.NotifyCollectionChangedAction.Reset))
End Set End Property Public ReadOnly Property PlugInUI As System.Windows.Controls.UserControl Implements Contract.API.IPlugIn.PlugInUI Get Dim myUI As New UserControl1 Return myUI End Get End Property
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChangedPublic Sub OnPropertyChanged(Optional propertyName As String = Nothing) RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(propertyName))
End Sub Public Event CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Implements System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged Public Sub OnCollectionChanged(args As System.Collections.Specialized.NotifyCollectionChangedEventArgs) RaiseEvent CollectionChanged(Me, args) End Sub End Class
PlugIn XAML
<Grid><ListBox Height="248" HorizontalAlignment="Left" Margin="30,31,0,0" Name="ListBox1" VerticalAlignment="Top" Width="241" ItemsSource="{Binding Files}"></Grid>
Host App
Imports System.ComponentModel.Composition Imports System.ComponentModel.Composition.Hosting Public Class HostApp <ImportMany(GetType(Contract.API.IPlugIn))> _ Public Property PlugIns As List(Of Contract.API.IPlugIn) Private _container As CompositionContainer Public Sub Compose() Dim catalog As New AggregateCatalog catalog.Catalogs.Add(New DirectoryCatalog("pluginfolder")) _container = New CompositionContainer(catalog) _container.ComposeParts(Me) End Sub Public Sub LoadPlugIns() Compose() For Each item In PlugIns Desktop.Children.Add(item.PlugInUI) Next End Sub Private Sub HostApp_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded LoadPlugIns() End Sub Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click Dim fileList As New Collections.ObjectModel.ObservableCollection(Of String) For Each fileItem As String In IO.Directory.GetFiles("Somefolder") fileList.Add(fileItem) Next PlugIns.Item(0).Files = fileList End Sub End Class
I need the Files property to be listed in the PlugIn ListBox when I click the HostApp Button.
I continue trying many things and found something really weird.
I added a button on the HostApp that get the count of string in Files PlugIn and it returns the quantity of string correctly.
I also added a button on the PlugIn that get the count of string in Files and it returns 0!
Why does the PlugIn exists in "two different instance" one in the host and one plugin-only?
Thanks for your help!