Xamarin表单-WebViewSource显示空白页

时间:2018-06-30 23:09:50

标签: c# android xamarin webview xamarin.forms

我在index.html内名为Posts1 - 10.html的文件夹中有CachedPostsEnvironment.GetFolderPath(Environment.SpecialFolder.Personal)。我的目标是加载index.html,该链接具有href链接以加载Posts1 - 10.html

您可能会告诉我使用Assets文件夹,但是{。Posts1 - 10.htmlindex.html是通过编程方式保存的。

C# Code

C# Code

所以我的问题出在“其他”区域。

index.html已加载,但是每当我单击index.html内的href链接时,它都会加载一个空白页:/

index.html Code

<html>
<body>
<a href="Posts1.html"><h2>Interesting Stuff 1</h2></a>
<a href="Posts2.html"><h2>Interesting Stuff 2</h2></a>
</body>
</html>

The Posts Code

<html>
<body>
<h1>Title: Posts Title</h1>
<p>So this is my first interesting post</p>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我查看了Forms的Android WebView渲染器,并且没有重新编写它以正确支持多模式内容,而渐进式Web应用程序,React托管等都需要它,您可以“只是”提供初始页面的URL,如果您有简单的缓存例程,则根本不使用HtmlWebViewSource / BaseUrl。

这意味着加载的第一个页面必须来自缓存位置,以及这些页面引用的所有其他内容,因此,如果您正在动态创建任何页面(包括第一个页面),则需要将它们保存到缓存中首先。

如果您使用的是Android,则可以使用Uri.Builder:

var url = new Android.Net.Uri.Builder()
                            .Scheme("file")
                            .Authority("localhost")
                            .AppendEncodedPath(CacheDir.CanonicalPath)
                            .AppendEncodedPath("index.html")
                            .Build();

或者只是通过以下格式的字符串对它进行硬编码:

$"file://localhost/{cacheDir}/index.html" 

$"file:///{cacheDir}/index.html" // skip localhost is it is implied

并使用带有UrlWebViewSource的结果网址:

webViewSource = new UrlWebViewSource { Url = $"file:///{cacheDir}/index.html" };
相关问题