【发布时间】:2014-03-07 13:13:49
【问题描述】:
我需要为 WebView 提供本地存储的 zip 文件中的内容。
我使用 NavigateToLocalStreamUri() 方法并提供我自己的扩展 UriResolver,其方式与 Windows 库链接中指向 NavigateToLocalStreamUri() 的示例相同。
但是,由于“NTLSUri”方法无法识别流的类型并尝试强制转换,因此调试非常失败。有谁知道如何正确地提供从压缩存档元素到 WebView 的流?
以下是详细信息:
这是我自己定制的 GetContent() 方法,用于向存档元素提供流而不是普通的。
private async Task<IInputStream> GetNormalZipEntryStream(String archivePath)
{
StorageFolder current = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("bookTest");
bookSourceFile = await current.GetItemAsync("testBook.zip") as StorageFile;
if (archivePath.ElementAt(0) == '/') archivePath = archivePath.Remove(0, 1);
Stream bookZipStream = await bookSourceFile.OpenStreamForReadAsync();
ZipArchive bookArchive = new ZipArchive(bookZipStream, ZipArchiveMode.Read);
ZipArchiveEntry bookContentFile = bookArchive.GetEntry(archivePath);
if (bookContentFile == null)
throw new Exception("Invalid archive entry");
else
{
try
{
IInputStream stream = bookContentFile.Open().AsInputStream();
return stream;
}
catch (Exception ex)
{
var streamConvErr = ex.Message;
}
}
return null;
}
现在,一切正常,我能够成功获取归档元素的流。我还确认 ArchiveElement.Open() 方法确实返回了一个带有“未压缩”内容的流。
问题在于 - 在幕后 - NavigateToStreamUri() 方法无法接受 bookContentFile.Open().AsInputStream() 返回的 IInput 流。
它会导致调试器在以下代码部分中断:
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
此时,e(例外)声明“无效的强制转换操作”。
还要注意,在调试过程中,在调用 AsInputStream() 时,流的类型与预期不同:IInputStream 类型,而是 System.IO.NetFxToWinRtStreamAdapter.InputStream 类型
扩展对象进一步将非公共成员“ManagedStream”视为 System.IO.Compression.DeflateStream 类型
我假设有一些方法可以使用 DataReader 将流转换为内存流,然后再转换回 IInputstream,但这似乎违反直觉,因为提供的 AsInpuSream() 方法应该按预期工作。
【问题讨论】:
标签: c# webview windows-runtime microsoft-metro windows-store-apps