Xamarin Forms - Displaying a local html file in webview on Android



Sounds simple but for some reason, it's not working for me. I have following code:

string rootPath = DependencyService.Get<IBaseUrl>().Get();
var source = new UrlWebViewSource
    Url = System.IO.Path.Combine(rootPath, "Index.html")

browser.Source = source;

and the code for IBaseUrl implementation on android is

public class BaseUrl : IBaseUrl
    public string Get()
        return "file:///android_asset/Content/";


Also tried removing 'Content' from the url but still not working.

1 Answer: 

You can read content from pcl and create html source:

public static string GetFileContent(string fileName) {
            string content;
            var assembly = typeof(App).GetTypeInfo().Assembly;
            var stream = assembly.GetManifestResourceStream(fileName);
            if (stream == null) return null;
            using (var reader = new StreamReader(stream)) content = reader.ReadToEnd();

            return content;

void CreateSource(){
     var htmlSource = new HtmlWebViewSource();
     htmlSource.Html = GetFileContent("AppName.Resources.Content.html");
     browser.Source = htmlSource;

This works for iOS and Android.