加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-8 – Windows 8 RT – 动态生成html不会渲染图像

发布时间:2020-12-14 02:49:50 所属栏目:Windows 来源:网络整理
导读:在LocalState目录中,我无法在网页上加载图像. 特别是,当文件路径引用LocalState目录时,尝试启动网页时似乎存在安全问题. 当我右键单击html文件并在Visual Studio中的浏览器中查看时,网页会加载图像. 我已将src标记的路径更改为:src =“ms-appdata:///Local
在LocalState目录中,我无法在网页上加载图像.

特别是,当文件路径引用LocalState目录时,尝试启动网页时似乎存在安全问题.

当我右键单击html文件并在Visual Studio中的浏览器中查看时,网页会加载图像.

我已将src标记的路径更改为:src =“ms-appdata:///Local/Logo.jpeg”
它不起作用.

帮我…

示例代码

public static async Task Update(WebView webview,IStorageFile file) { 
  var html = await Windows.Storage.PathIO.ReadTextAsync(file.Path);
  webview.NavigateToString(html);
 }

解决方法

NavigateToString方法不适用于指向LocalData文件夹中的图像的标记. (据我所知,无论如何).实际上NavigateToString也会破坏JavaScript和CSS链接.

服务器上的图像

一种解决方案是将源更改为指向网络服务器而不是localdata.我不确定它是否适用于您的应用场景.

图像和HTML作为内容

第二种选择是将您的html和图像文件作为内容添加到您的应用程序并使用

WebView1.Navigate(new Uri("ms-appx-web:///assets/SampleHtmlPage.html"));

加载HTML.

在进程HTTP服务器中

这是一个在应用程序中使用自定义HTTP服务器来处理问题的解决方案.

Loading Local HTML Content in Metro WebView (Windows 8)

基础64编码图像

最后,还有另一种解决方案,使用LocalData文件夹中的图像的Base64编码.

internal async void MakeHtmlString()
{
  StorageFile imageFile; // get image file here.

  var html = 
     string.Format("<div><img src='data:image/png;base64,{0}'",await GetImageBase64(imageFile));
}

internal async Task<string> GetImageBase64(StorageFile imageFile)
{
  var imageStream = await imageFile.OpenAsync(FileAccessMode.Read);
  var inputStream = imageStream.GetInputStreamAt(0);
  var dataReader = new DataReader(inputStream);

  var dataResults = await dataReader.LoadAsync((uint)imageStream.Size);
  var bytes = new byte[dataResults];
  dataReader.ReadBytes(bytes);
  return Convert.ToBase64String(bytes);
}

最后一种方法适用于图像,但不适用于CSS文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读