Windows应用商店8.1:更改流时,WebView.NavigateToStream不起作用

时间:2013-10-02 00:26:52

标签: xaml webview windows-store-apps c#-5.0 windows-8.1

我目前正在尝试使用WebView显示加密数据(使用DPP)。问题是,如果在将StorageFile读入IRandomAccessStream(即未加密数据)后以任何方式操作流,我无法让WebView显示任何内容。

使用Microsoft提供的来源可以轻松复制此问题:http://code.msdn.microsoft.com/windowsapps/XAML-WebView-control-sample-58ad63f7

并更改“StreamUriWinRTResolver”类以将IRandomAccessStream转换为内存流然后再返回。基本上,在第128行的S4_NavToStream.xaml.cs中,更改:

StorageFile f = item as StorageFile;
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);

有了这个:

StorageFile f = item as StorageFile;
IRandomAccessStream randStream = await f.OpenAsync(FileAccessMode.Read);
var stream = randStream.AsStream();
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
return ms.AsInputStream();

这将在运行时显示空白页面。我开始认为这是一个错误,除非我当然错误地使用了流转换。有没有人尝试过类似的东西?

1 个答案:

答案 0 :(得分:0)

Thomas Huber在social.msdn.microsoft.com上非常慷慨地回答了我的问题。完整答案:

http://social.msdn.microsoft.com/Forums/en-US/f51ab699-66c3-4961-8952-fbe341bf3e23/windows-81-webviewnavigatetostream-does-not-work-when-altering-the-stream

总之,问题在于将内存流转换为输入流。这是正确的工作代码:

StorageFile f = item as StorageFile;
IRandomAccessStream randStream = await f.OpenAsync(FileAccessMode.Read);
var stream = randStream.AsStream();
var ms = new MemoryStream();
stream.CopyTo(ms);

InMemoryRandomAccessStream msras = new InMemoryRandomAccessStream();
DataWriter dw = new DataWriter(msras);
dw.WriteBytes(ms.ToArray());
await dw.StoreAsync();
var inputStream= msras.GetInputStreamAt(0);
return inputStream;