Re: Dynamically load Assemblies ... Assembly.Load() ... AssemblyPart.Load() ... Silverlioght 2.0
This thread explains exactly what I need to do. Looking at SilverlightDesktop.net makes me realise how similar my project is... but I just can't get it working.
I can get the WebClient object to retrieve the assembly and feed the stream into an AssemblyPart object's Load method. The assembly is loaded correctly and stored in the Assembly object and I can even create an instance of the class I need (checked that it is not null with the debugger), but when I try to assign the Content property of a TabItem with the created instance (which is actually a subclass of UserControl), it just comes up blank and not the test text that is actually in the UserControl. No errors, nothing. It's like the UserControl loses everything it contains. Here's what I'm currently working with.
WebClient handler:void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// Debug information
((TabItem)tabs.SelectedItem).Content = String.Format("{0} - {1} - {2}", (e.Error == null), e.Error, e.Result);
// Create new AssemblyPart object and load assembly into a new Assembly object
AssemblyPart ap = new AssemblyPart();
Assembly a = ap.Load(e.Result);
// Create instance of ProjectsModule and set as currently selected TabItem's Content
((TabItem)tabs.SelectedItem).Content = a.CreateInstance("Project.Modules.ProjectsModule") as UserControl;
}
ProjectsModule.xaml
<UserControl x:Class="Project.Modules.ProjectsModule"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock>Test String</TextBlock>
</Grid>
</UserControl>
ProjectsModule.xaml.cs
namespace Project.Modules
{
public partial class ProjectsModule : UserControl
{
public ProjectsModule()
{
InitializeComponent();
}
}
}
Hopefully one of you guys can see someone obvious that I'm doing wrong ;) I can't understand why I'm just getting blank content even when the debugger shows a ProjectsModule object being created from the CreateInstance method.
Thanks in advance!