【问题标题】:Problem with dynamic loading XAP files with MEF. IE is crashing使用 MEF 动态加载 XAP 文件时出现问题。 IE 崩溃了
【发布时间】: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


【解决方案1】:

我设法创建了一个快速的 Silverlight 业务应用程序,并在几分钟内让您的代码运行起来。

我没有将代码放在 MainPage 中,而是将其放在 Home Page 中。我认为这不会影响任何事情 - 商务应用主页只是有点拥挤。

我确实移动了 CompositionHost.Initialize(catalog);到 DownloadAsync 之前,尽管它仍然可以正常工作。

我认为你可能做错的唯一一件事是你有一个用于 Test2 项目的 Silverlight 类库,但实际上并没有一个能生成 Test2.xap 的 Silverlight 应用程序项目来放入你的ClientBin(在您的图片中未展开)。

对不起,如果它没有帮助,但您的代码实际上正在运行。

我什至创建了第二个项目应用程序,添加到目录和下载,两个项目都被下载了。

希望您发现了您的问题。

LJ

【讨论】:

  • Hello Test2 是一个 Silverlight 应用程序,但是我已经删除了 App.xaml 和 MainPage.xaml,因为我听说它们不需要。当我构建应用程序时,我确实得到了两个 .XAP 文件。我做了与你上面描述的完全相同的步骤,我遇到了同样的问题。我还添加了一些代码来调试它,但没有成功。 (Se main post)。
  • 我尝试在 Opera 中运行它,我收到了更详细的错误消息。更多详情请查看主帖。
【解决方案2】:

我试图从第一步开始做所有事情,希望我错过了什么。我怎么没有。即使在一个新的干净项目中也有同样的问题。然后我使用 Opera 而不是 IE。它给了我一个更好的错误。

我决定尝试用 opera 运行 Main 应用程序,它神奇地工作。 这给了我清除 IE 缓存并重新启动应用程序以及重新启动 ASP.NET 开发服务器的想法。该应用程序也适用于 IE。

所有这一切都无需更改一行代码。所以总而言之,我认为 IE 和缓存一直是问题所在。

编辑:现在我在 IE 中遇到同样的旧错误(停止工作),但它在歌剧中工作......

【讨论】:

  • 为什么我现在不能将此答案设置为我接受的答案?我要等 19 个小时?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多