【发布时间】:2010-08-12 07:37:21
【问题描述】:
当我尝试动态加载 XAP 文件时,我遇到了一个巨大的 MEF 问题。当我使用方法调用 dc.DownloadAsync(); 下载目录/xap 文件时;我的 Internet Explorer 将崩溃并显示“IE 已停止工作”对话框。 (看图片亲爱的)。
我已经按照步骤指南进行了几个步骤,但我只是看不出我做错了什么或错过了什么。
我的解决方案资源管理器概览 (请在帖子末尾查看图片以获得更详细的视图):
解决方案“AppStation”
-
AppStation.Common
- IApp.cs
-
AppStation.GUI
- App.xaml
- MainPage.xaml
-
测试2
- HelloMefApp.cs
界面:
public interface IApp
{
string Name { get; }
string Description { get; }
UserControl GetUserInterface();
}
实施:
[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、动态加载 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();
}
主页:
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());
}
}
}
}
(来源:colincochrane.com)
LJ 回答后更新: Test2 是一个 Silverlight 应用程序,但我已经删除了 App.xaml 和 MainPage.xaml,因为我听说它们不需要。当我构建应用程序时,我确实得到了两个 .XAP 文件。
我做了与你上面描述的完全相同的步骤,我遇到了同样的问题。
我还尝试通过添加以下代码行来进一步调试它:
dc.DownloadCompleted += (s, args) =>
{
int x = 10;
};
dc.DownloadProgressChanged += (s, args) => {
int x = 10;
};
我注意到的只是我的断点(我为每个事件添加了一个)没有被命中。
更新:尝试使用opera,并获得了更好的Expetion消息:
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)
【问题讨论】:
-
你提到了错误的图像,但我没有看到它。
-
当我第一次发帖时,我试图包含图片,但由于这是我的第一篇文章,我没有足够的声誉(?)来发布它们。但是现在我有,现在它们被添加了。
标签: .net silverlight silverlight-4.0 mef