代码之家  ›  专栏  ›  技术社区  ›  Entity

Application.GetResourceStream抛出IOException

  •  0
  • Entity  · 技术社区  · 14 年前

    我正在开发一个版本历史对话框,我创建了一个示例来测试它。但是,示例似乎找不到HTML文件:

            var dlg = new VersionHistoryDialog();
            var uri = new Uri(@"pack://application:,,,/VersionHistory.html", UriKind.Absolute);
            var source = Application.GetResourceStream(uri).Stream; // This line throws the error
            dlg.Stream = source;
            var result = dlg.ShowDialog();
            label1.Content = result;
    

    上面代码中的那一行抛出此错误:

    System.IO.IOException was unhandled
      Message=Cannot locate resource 'versionhistory.html'.
      Source=PresentationFramework
      StackTrace:
           at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
           at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
           at System.IO.Packaging.PackagePart.GetStream()
           at System.Windows.Application.GetResourceStream(Uri uriResource)
        ....
    

    VersionHistory.html 和它在同一个文件夹(“视图”)中 xaml.cs 文件要求它。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Pieter van Ginkel    14 年前

    您需要包含资源的程序集和路径:

    例如:

    Application.GetResourceStream(new Uri("/SilverlightApplication;component/EmbeddedInApplicationAssembly.png", UriKind.Relative)))
    

    Application.GetResourceStream(new Uri("pack://application:,,,/View/versionhistory.html"))
    

    下面的方法也应该有效:

    Application.GetResourceStream(new Uri("/XYZ;component/View/versionhistory.html", UriKind.Relative)))
    

    http://msdn.microsoft.com/en-us/library/ms596994(VS.95).aspx http://msdn.microsoft.com/en-us/library/aa970069.aspx 更多信息。