Hello, i am having a huge problem with MEF when i am trying to dynamic load XAP files. When i am download the catalog/xap file with the the method call dc.DownloadAsync(); My Internet explorer will crash and show me the "IE has stopped working" dialog. (see image belove).
I have followed several steps by steps guides and i just cant see what i am doing wrong or what i have missed.
OverView of my Solution Explorer (Se image in the end of the post for a more detailed view):
Solution 'AppStation'
- AppStation.Common
- IApp.cs
- AppStation.GUI
- App.xaml
- MainPage.xaml
- Test2
- HelloMefApp.cs
Interface:
public interface IApp
{
string Name { get; }
string Description { get; }
UserControl GetUserInterface();
}
Implementation:
[Export(typeof(IApp))]
public class HelloMefApp : IApp
{
public string Name
{
get { return "Hello MEF"; }
}
public string Description
{
get { return "Adds a label with the text 'Hello MEF'"; }
}
public UserControl GetUserInterface()
{
UserControl uc = new UserControl();
TextBlock textBlock = new TextBlock();
textBlock.Text = "Hello MEF";
uc.Content = textBlock;
return uc;
}
}
App.xaml.cs, Application_Startup, Dynamic loading the XAP:
private void Application_Startup(object sender, StartupEventArgs e)
{
AggregateCatalog catalog = new AggregateCatalog();
DeploymentCatalog dc = new DeploymentCatalog(new Uri("Test2.xap", UriKind.Relative));
catalog.Catalogs.Add(dc);
dc.DownloadAsync(); //This will give the "Internet Explorer has stopped working" crash.
CompositionHost.Initialize(catalog);
this.RootVisual = new MainPage();
}
MainPage:
public partial class MainPage : UserControl, IPartImportsSatisfiedNotification
{
[ImportMany(AllowRecomposition = true)]
public IApp[] Apps { get; set; }
public MainPage()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
}
public void OnImportsSatisfied()
{
if (Apps != null)
{
foreach (IApp item in Apps)
{
LayoutRoot.Children.Add(item.GetUserInterface());
}
}
}
}
Update after LJ answer: Test2 is a Silverlight Application, i have howeever removed the App.xaml and MainPage.xaml since i heard they were not needed. and when i build the application i do indeed get two .XAP files.
I did the exact same steps as you described above and i am getting the same problem.
I have also tried to debug it a bit further by adding these lines of codes:
dc.DownloadCompleted += (s, args) =>
{
int x = 10;
};
dc.DownloadProgressChanged += (s, args) => {
int x = 10;
};
And all i notices is that my breakpoints (i added one to each event) is not getting hit.
Update: Tried with opera, and got a better Expetion message:
Exception has been Thrown by the target of an invocation.
at System.Windows.Navigation.PageResourceContentLoader.EndLoad(IAsyncResult asyncResult)
at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
Caused by: Exception has been thrown by the target of an invocation.
at System.Windows.Navigation.PageResourceContentLoader.EndLoad(IAsyncResult asyncResult)
at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)