简体   繁体   English

InMemory HTML 和图像 .NET MAUI WebView

[英]InMemory HTML and Images for .NET MAUI WebView

WebView webView = new WebView
{
    Source = new HtmlWebViewSource
    {
        Html = @"<!DOCTYPE html><HTML>
<BODY>
<H1>.NET MAUI</H1>
<P>Welcome to WebView.</P>
<img src=""myimg.jpg"" alt=""My Image"" width=""500"" height=""600"">
</BODY>
</HTML>",
    },
};

I wanted to write something like this.我想写这样的东西。 Having HTML -string in memory to be displayed.显示HTML中的 HTML 字符串。 Images, Audios and potentially Vidoes should also be loaded from memory.图像、音频和潜在的视频也应该从 memory 加载。

What must I do to add an Handler to webView or webView.Source that is called, when the image should be loaded so that I can return an byte[] with the data should be interpreted as image, sound or video?我必须做什么才能将Handler添加到webViewwebView.Source中,当图像应该被加载时,我可以返回一个byte[]数据应该被解释为图像、声音或视频?

I have search in the web and found some ideas我在 web 中搜索并找到了一些想法

webView.Source.Load(new MauiWKWebView());

But I have no ASP.NET project... 但是我没有ASP.NET项目...

It works in this [example][1] if images are added as static resource.如果将图像添加为 static 资源,它会在此 [example][1] 中工作。 But my media data is stored in a database.但是我的媒体数据存储在数据库中。

While using WebView , the only way to render image I'm avare of is to set it's content as base64 string.在使用WebView时,我所知道的渲染图像的唯一方法是将其内容设置为base64字符串。
Doing so, your example would look something like this:这样做,您的示例将如下所示:

var htmlString = @"<!DOCTYPE html>
<HTML>
<BODY>
<H1>.NET MAUI</H1>
<P>Welcome to WebView.</P>
<img src=""data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="" alt=""Red dot"">
</BODY>
</HTML>";

var webView = new WebView
{
    Source = new HtmlWebViewSource
    {
      Html = htmlString
    }
};

Assuming you have image data in byte[] form, you can use this to convert it to base64 string :假设您有byte[]形式的图像数据,您可以使用它来将其转换为base64 string

var imageBytes = ...;
var base64ImageString = System.Convert.ToBase64String(imageBytes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM