Hello,
Welcome to our Microsoft Q&A platform!
You can create a custom webview add two BindableProperty in the background code.
public class MyWebview:WebView
{
public static BindableProperty AllowContentAccessProperty =
BindableProperty.Create(nameof(AllowContentAccess),
typeof(bool),
typeof(MyWebview),
null);
public bool AllowContentAccess
{
get => (bool)GetValue(AllowContentAccessProperty);
set => SetValue(AllowContentAccessProperty, value);
}
public static BindableProperty AllowFileAccessProperty =
BindableProperty.Create(nameof(AllowFileAccess),
typeof(bool),
typeof(MyWebview),
null);
public bool AllowFileAccess
{
get => (bool)GetValue(AllowFileAccessProperty);
set => SetValue(AllowFileAccessProperty, value);
}
}
Then use it in the xaml.
<customwebviewdemo:MyWebview AllowContentAccess="True" AllowFileAccess="True" Source="https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry" WidthRequest="100" HeightRequest="100" ></customwebviewdemo:MyWebview>
In the android customrenderer, you can set the Bindable value.
[assembly: ExportRenderer(typeof(MyWebview), typeof(MyWebviewRenderer))]
namespace CustomWebviewDemo.Droid
{
class MyWebviewRenderer : WebViewRenderer
{
Context context;
public MyWebviewRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
var webView = (global::Android.Webkit.WebView)Control;
MyWebview myWebview= e.NewElement as MyWebview;
webView.Settings.AllowContentAccess = myWebview.AllowContentAccess;
webView.Settings.AllowFileAccess = myWebview.AllowFileAccess;
}
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.