5
votes

In my shared portable Xamarin project, this works on UWP, Windows Phone 8.1 and Windows 8.1:

HtmlWebViewSource htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<html><body><img src='ms-appx-web:///Assets/somePicture.png' /></body></html>";
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
WebView webView = new WebView
{
        Source = htmlSource,
};

Obviously, this isn't cross-platform (iOS and Android). I want this, but it doesn't work on UWP, Windows Phone 8.1 and Windows 8.1:

HtmlWebViewSource htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<html><body><img src='somePicture.png' /></body></html>";
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
WebView webView = new WebView
{
        Source = htmlSource,
};

IBaseUrl:

public interface IBaseUrl { string Get(); }

BaseUrl implementation for UWP, Windows Phone 8.1 and Windows 8.1, taken from a Windows Phone 8.0 sample project:

[assembly: Dependency(typeof(MyApp.UWP.BaseUrl))]
namespace MyApp.UWP
{
    public class BaseUrl : IBaseUrl
    {
        public string Get()
        {
            return ""; // "ms-appx-web:///Assets/" doesn't work
        }
    }
}

I've tried different variations of returning BaseUrl of "ms-appx-web:///Assets/", "ms-appx-web:///", placing the files in the project root or in the “Assets” dir, nothing works.

As far as I can tell, this used to work on Windows Phone 8.0.

2

2 Answers

3
votes

What about Device.OnPlatform?

var urlIos = @"somePicture.png";
var urlAndroid = @"somePicture.png";
var urlUWP = @"ms-appx-web:///Assets/somePicture.png";
htmlSource.Html = string.Format(@"<html><body><img src='{0}' /></body></html>", Device.OnPlatform(urlIos, urlAndroid, urlUWP));