Webview BaseURL in Xamarin.Forms on UWP and Windows Phone 8.1

In my general Xamarin portable 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 is not cross-platform (iOS and Android). I want this, but it does not 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 Windows Phone 8.0 project example :

 [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 tried different options for returning BaseUrl from ms-appx-web: /// Assets / "," ms-appx-web: /// ", placing files in the root of the project or in" Assets ", nothing works.

As far as I can tell, this worked on Windows Phone 8.0.

+5
source share
2 answers

Starting with Xamarin Forms 2.3.1, WebViewRenderer for Windows RT simply ignores BaseUrl ( https://github.com/xamarin/Xamarin.Forms/blob/2.3.1/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs#L26 ) . However, there is a fix for this on branch 2.3.2: https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs

+1
source

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)); 
+3
source

Source: https://habr.com/ru/post/1247880/


All Articles