UWP和Windows Phone 8.1上Xamarin.Forms中的Webview BaseURL [英] Webview BaseURL in Xamarin.Forms on UWP and Windows Phone 8.1
问题描述
在我的共享便携式Xamarin项目中,这适用于UWP,Windows Phone 8.1和Windows 8.1:
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,
};
很明显,这不是跨平台的(iOS和Android).我想要这个,但不适用于UWP,Windows Phone 8.1和Windows 8.1:
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(); }
用于UWP,Windows Phone 8.1和Windows 8.1的BaseUrl实施,取自 Windows Phone 8.0示例项目:
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
}
}
}
我尝试了不同的变体,分别返回"ms-appx-web:///Assets/","ms-appx-web:///"的BaseUrl,并将文件放置在项目根目录或资产"目录,没有任何作用.
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.
据我所知,这曾经在Windows Phone 8.0上有效.
As far as I can tell, this used to work on Windows Phone 8.0.
推荐答案
从Xamarin Forms 2.3.1开始,用于Windows RT的WebViewRenderer只是忽略了BaseUrl( https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs
As of Xamarin Forms 2.3.1, WebViewRenderer for Windows RT simply ignores the 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 the 2.3.2 branch: https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs
这篇关于UWP和Windows Phone 8.1上Xamarin.Forms中的Webview BaseURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!