加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

UWP和Windows Phone 8.1上的Xamarin.Forms中的Webview BaseURL

发布时间:2020-12-14 01:38:05 所属栏目:Windows 来源:网络整理
导读:在我的共享便携式Xamarin项目中,这适用于UWP,Windows Phone 8.1和Windows 8.1: HtmlWebViewSource htmlSource = new HtmlWebViewSource();htmlSource.Html = @"htmlbodyimg src='ms-appx-web:///Assets/somePicture.png' //body/html";htmlSource.BaseUrl =
在我的共享便携式Xamarin项目中,这适用于UWP,Windows Phone 8.1和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:

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 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的不同变体,将文件放在项目根目录或“Assets”目录中什么都行不通

据我所知,这曾经适用于Windows Phone 8.0.

解决方法

从Xamarin Forms 2.3.1开始,用于Windows RT的WebViewRenderer只是忽略了BaseUrl( https://github.com/xamarin/Xamarin.Forms/blob/2.3.1/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs#L26).但是,在2.3.2分支上有一个修复: https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读