Application.GetResourceStream抛出IOException

时间:2010-10-22 12:30:55

标签: c# stream ioexception

我正在使用“版本历史记录”对话框,我创建了一个示例来测试它。但是,似乎示例无法找到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 个答案:

答案 0 :(得分:3)

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

例如:

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).aspxhttp://msdn.microsoft.com/en-us/library/aa970069.aspx