How can I load html file from arbitrary folder with WebView

caseyryancaseyryan Member ✭✭
edited March 25 in Xamarin.Forms

Hi!
I need to load local HTML file which I generate on the fly.
So I've created a simple page containing a webview
<?xml version="1.0" encoding="utf-8" ?><br /> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DelivApp.Views.TrackOrdersPage"><br /> <WebView x:Name="MapWebView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="{Binding Path=MapSource}" /><br /> </ContentPage><br />
And the code behind

using System.Diagnostics;
using DelivApp.Tools;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DelivApp.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TrackOrdersPage : ContentPage {
        private UrlWebViewSource m_MapSource;

        public TrackOrdersPage () {
            InitializeComponent ();
            BindingContext = this;
            Utils.GenerateMapWrapper();
            var mapPath = Utils.GetMapWrapperPath();
            Debug.WriteLine(mapPath);

            MapSource = new UrlWebViewSource() {
                Url = mapPath
            };

        }

        public UrlWebViewSource MapSource {
            get { return m_MapSource; }
            set {
                m_MapSource = value; 
                OnPropertyChanged();
            }
        }
    }
}

The code that generates the file

public static void WriteLocalFile(string fileName, string contents, string extension = "txt") {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var filePath = Path.Combine(path, $"{fileName}.{extension}");

            using (var streamWriter = new StreamWriter(filePath, false)) {
                streamWriter.WriteLine(contents);
            }

        }

        public static string ReadLocalFile(string fileName, string extension = "txt") {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var filePath = Path.Combine(path, $"{fileName}.{extension}");

            using (var streamReader = new StreamReader(filePath)) {
                return streamReader.ReadToEnd();
            }
        }

        public static bool LocalFileExists(string fileName, string extension = "txt") {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            return File.Exists(path);
        }

        public static string GetMapWrapperPath() {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    // todo Adnroid only, replace with a platform dependency
            return "file://" + Path.Combine(path, $"map.html");
        }

        public static void GenerateMapWrapper() {

            var html = [email protected]"<!DOCTYPE html>
                <html>
                <head>
                <title>Page title</title>
                <meta http-equiv = ""Content-Type"" content = ""text/html; charset=utf-8\"" />
            </head>
            <body>
                <p>Some text</p>
            </body>
            </html>";

            WriteLocalFile("map", html, "html");
        }

The result is that there is nothing in a webview. If I change the Url to e.g. https://google.com it loads google's page with no problems.
What am I doing wrong? Is it possible at all to load a local html page like this?

p.s. I've seen all those threads which show how to load an HTML file embedded to the application assets folder but it's not the case

Best Answer

  • caseyryancaseyryan ✭✭
    edited March 25 Accepted Answer

    My bad. The code is correct now, I've just noticed that I wasn't writing content to the file, only the file itself. I gotta get some rest today :)

Answers

  • caseyryancaseyryan Member ✭✭
    edited March 25 Accepted Answer

    My bad. The code is correct now, I've just noticed that I wasn't writing content to the file, only the file itself. I gotta get some rest today :)

Sign In or Register to comment.