2010-10-22 138 views
0

我正在處理版本歷史記錄對話框,並且我創建了一個樣本來測試它。不過,看來,該樣品找不到HTML文件:Application.GetResourceStream拋出IOException

 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文件相同的文件夾(「視圖」)中詢問它。

回答

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

+0

所以/ SilverlightApplication;部分將是項目的名稱?假設我在文件夾'Views'下的Project'XYZ'中有文件。我將如何進入? – Entity 2010-10-22 12:46:59

相關問題