在 webview 中加载本地 HTML
appfolder = ApplicationData.Current.LocalFolder;
xmlFile = await appfolder.GetFileAsync("000Title.html");
using (Stream stream = await xmlFile.OpenStreamForReadAsync())
{
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
//stream.Read
XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = true;
settings.DtdProcessing = DtdProcessing.Ignore;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (await reader.ReadAsync())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "img")
{
string attributeValue = reader.GetAttribute("src"); // img elemeent
string val = attributeValue;
StorageFile file = await appfolder.GetFileAsync(attributeValue);
using (var stream1 = await file.OpenAsync(FileAccessMode.Read))
{
var reader1 = new DataReader(stream1.GetInputStreamAt(0));
var bytes = new byte[stream1.Size];
await reader1.LoadAsync((uint)stream1.Size);
reader1.ReadBytes(bytes);
string base64 = Convert.ToBase64String(bytes);
string Base = "data:image/jpg;base64," + base64;
}
在这里,我将属性“Src”值替换为 base64 字符串。
attributeValue = attributeValue.Replace(attributeValue,Base);
html = await FileIO.ReadTextAsync(xmlFile);
view.NavigateToString(html);
在这里,我在 webview 中加载 HTML,但它显示的是以前的值。
更新:不要指定使用像 Html Agility Pack 这样的库
请帮帮我。
谢谢。