Xamarin Forms - WebViewSource shows blank page
Xamarin Forms - WebViewSource shows blank page
I have index.html and Posts1 - 10.html inside a folder called CachedPosts inside Environment.GetFolderPath(Environment.SpecialFolder.Personal). My goal is to load index.html which has href links to load Posts1 - 10.html.
index.html
Posts1 - 10.html
CachedPosts
Environment.GetFolderPath(Environment.SpecialFolder.Personal)
index.html
Posts1 - 10.html
You may tell me to use the Assets folder, but.. Posts1 - 10.html & index.html are being saved programmatically.
Posts1 - 10.html
index.html
C# Code:
C# Code

So my issue lies in the "else" area.
index.html loads, but whenever I click on the href link inside index.html, it loads a blank page :/
index.html
index.html
index.html Code:
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:
The Posts Code
<html>
<body>
<h1>Title: Posts Title</h1>
<p>So this is my first interesting post</p>
</body>
</html>
1 Answer
1
I looked at the Forms' Android WebView renderer and without re-writting it to properly support multi-mode content which you would need for a progressive web app, React hosting, etc..., you can "just" supply a URL for the initial page and not use the HtmlWebViewSource/BaseUrl at all if you have a simple caching routine.
WebView
This means that the first page loaded has to come from your cache location, along with all other content that those pages refer to so if you are dynamically creating any pages, including that first one, they need to be saved into the cache first.
If you are on Android, you could use Uri.Builder:
var url = new Android.Net.Uri.Builder()
.Scheme("file")
.Authority("localhost")
.AppendEncodedPath(CacheDir.CanonicalPath)
.AppendEncodedPath("index.html")
.Build();
Or just hard-code it via format string via:
$"file://localhost/{cacheDir}/index.html"
or
$"file:///{cacheDir}/index.html" // skip localhost is it is implied
And use the resulting url with a UrlWebViewSource:
UrlWebViewSource
webViewSource = new UrlWebViewSource { Url = $"file:///{cacheDir}/index.html" };
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
THANKS! This worked!
– Denise
Jul 1 at 10:04