I'm trying to figure out how to make MEF work (just started with it). I've managed to write a code where I can import my xap part and use it in my main application. But my parts need parameters for database queries so I'll need a way to pass them. I've read that I should be using "ImportingConstructor" and this is what I'm planning on trying next. But I don't know what to do when composing the part and need some help to make this work.
Here is my export class code:
<Export(GetType(UserControl))> _ Partial Public Class MainPage Inherits UserControl Private Shared Property m_Id As Integer Public Shared ReadOnly Property Id As Integer Get Return m_Id End Get End Property Public Sub New() InitializeComponent() End Sub <ImportingConstructor()> Public Sub New(<Import(AllowDefault:=True)> ByVal pId As Integer) InitializeComponent() m_Id = pId End Sub
And below is how I'm importing it. Where should I put the integer value I need to pass to the constructor?
Partial Public Class MainPage Inherits UserControl <ImportMany(AllowRecomposition:=True)> _ Public Property ImportedParts As New List(Of Lazy(Of UserControl)) Public Sub New() InitializeComponent() End Sub Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded Dim catalogs As New AggregateCatalog() Dim catalog1 As New DeploymentCatalog(New Uri("Import1.xap", UriKind.Relative)) catalogs.Catalogs.Add(catalog1) AddHandler catalog1.DownloadCompleted, AddressOf DownloadCompleted catalog1.DownloadAsync() CompositionHost.Initialize(catalogs) CompositionInitializer.SatisfyImports(Me) End Sub Private Sub DownloadCompleted(sender As Object, e As AsyncCompletedEventArgs) Dim mefModule As Lazy(Of UserControl) = (From m In ImportedParts Where m.Value.ToString().Contains("MainPage") Select m).FirstOrDefault() If mefModule IsNot Nothing Then Dim control As UserControl = mefModule.Value Me.StackPanel1.Children.Add(control) End If End Sub End Class
Thank you for your help :)
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog