当我尝试动态加载XAP文件时,我遇到了MEF的巨大问题。当我使用方法调用dc.DownloadAsync()下载catalog / xap文件时;我的Internet Explorer将崩溃并向我显示“IE已停止工作”对话框。 (见图像belove)。
我已经按照步骤指南执行了几个步骤,我只是看不出我做错了什么或我错过了什么。
我的解决方案资源管理器的OverView (有关更详细视图的帖子末尾的图像):
解决方案'AppStation'
接口
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();
}
的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());
}
}
}
}
alt text http://www.freeimagehosting.net/uploads/cf93454c1a.jpg
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)
答案 0 :(得分:1)
我设法创建了一个快速的Silverlight业务应用程序,并在几分钟内运行您的代码。
不是将代码放在MainPage中,而是将其放在主页中。我认为这不会影响任何事情 - 商业应用主页只是有点拥挤。
我确实移动了CompositionHost.Initialize(目录);在DownloadAsync之前,尽管它仍然有效。
我唯一可以认为你可能做错的事情就是你有一个用于Test2项目的Silverlight类库,并且实际上没有一个Silverlight应用程序项目可以生成一个Test2.xap来放入你的ClientBin(未在图片中展开)。
很抱歉,如果它无效,但您的代码实际上正在运行。
我甚至创建了第二个项目应用程序,添加了目录和下载,并且下载了这两个项目。
希望你发现了自己的问题。
LJ
答案 1 :(得分:1)
我试图从第一步开始,希望我错过了一些东西。我怎么没有。即使在新的清洁项目中也存在相同的问题然后我使用Opera而不是IE。它给了我一个更好的错误。
我决定尝试用Opera运行Main应用程序,它神奇地工作。 这让我想清除我的IE缓存并重启应用程序以及重新启动ASP.NET开发服务器。应用程序也适用于IE。
所有这一切都没有改变一行代码。总而言之,我认为IE和缓存一直都是问题。
编辑:现在我在IE中遇到了同样的旧错误(停止工作),但它在Opera中工作....