【发布时间】:2020-04-27 05:21:29
【问题描述】:
我在 Xamarin 中实现了一个 webview 页面,它带有一个自定义渲染器来放大/缩小页面。问题是 android 中的 WebView 不允许保存任何已加载到 webView 中的图像,但是使用相同的代码我可以在 iOS 上下载图像。
有什么办法,当用户长按 Android 的 WebView 上的图像时,我可以保存图像吗?提前致谢。
这是我正在使用的代码。
WebViewCustom
this._WebView = new WebViewCustom
{
BackgroundColor = AppConfig.MenuBackgroundColor,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Source = new UrlWebViewSource
{
Url = url
},
};
WebViewCustomRenderer 类
[assembly: ExportRenderer(typeof(WebViewCustom), typeof(WebViewCustomRenderer))]
namespace PBL.App.Droid.Renderers
{
class WebViewCustomRenderer : WebViewRenderer
{
public WebViewCustomRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Control != null)
{
Control.Settings.BuiltInZoomControls = true;
Control.Settings.DisplayZoomControls = false;
Control.Settings.SetSupportZoom(true);
Control.Settings.AllowFileAccess = true;
MessagingCenter.Subscribe<object, bool>(this, "zoom", (sender1, arg) => {
Control.Settings.SetSupportZoom(arg);
});
}
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
if (Control != null)
{
Control.Settings.SetSupportZoom(true);
}
if(e.NewElement != null)
{
Control.Settings.AllowContentAccess = true;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.Settings.DomStorageEnabled = true;
Control.Settings.JavaScriptEnabled = true;
Control.Download += OnWebViewDownload;
}
base.OnElementChanged(e);
}
private void OnWebViewDownload(object sender, DownloadEventArgs e)
{
var source = Uri.Parse(e.Url);
var request = new DownloadManager.Request(source);
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
request.SetDestinationInExternalPublicDir(Environment.DirectoryDownloads, source.LastPathSegment);
}
}
}
【问题讨论】:
-
可以吗?
标签: android xamarin.forms webview webclient