【问题标题】:Xamarin Forms - WebViewSource shows blank pageXamarin Forms - WebViewSource 显示空白页
【发布时间】:2018-06-30 23:09:50
【问题描述】:

我在Environment.GetFolderPath(Environment.SpecialFolder.Personal) 中的一个名为CachedPosts 的文件夹中拥有index.htmlPosts1 - 10.html。我的目标是加载index.html,其中包含要加载Posts1 - 10.html的href链接。

您可以告诉我使用 Assets 文件夹,但是.. Posts1 - 10.htmlindex.html 正在以编程方式保存。

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>

【问题讨论】:

    标签: c# android xamarin webview xamarin.forms


    【解决方案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
    

    并将生成的 url 与 UrlWebViewSource 一起使用:

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

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 2014-09-13
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      相关资源
      最近更新 更多