问题描述
我有一个简单的Xamarin页面,其中包含一个调用WebRTC测试页面的WebView:
I have a simple Xamarin Page with a WebView that calls a WebRTC test page:
_webView = new WebView
{
Source = "https://test.webrtc.org/",
WidthRequest = 1000,
HeightRequest = 1000
};
var stackLayout = new StackLayout()
{
Orientation = StackOrientation.Vertical,
Padding = new Thickness(5, 20, 5, 10),
Children = { _webView }
};
Content = new StackLayout { Children = { stackLayout } };
https://test.webrtc.org/页在同一Android上的Chrome上运行良好模拟器,但不能在WebView上显示"NotAllowedError".
The https://test.webrtc.org/ page works fine on Chrome on the same Android Emulator, but don't work on WebView saying "NotAllowedError".
该应用程序具有所需的权限.以下代码(使用Plugin.Permissions)返回true:
The application have the required permissions. The following code (that use Plugin.Permissions) returns true:
var statusCamera = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
var statusMicrophone = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Microphone);
return statusCamera == PermissionStatus.Granted && statusMicrophone == PermissionStatus.Granted;
怎么了?
谢谢
推荐答案
关于NotAllowedError
,来自此处:
您需要自定义WebView
来覆盖WebChromeClient
的OnPermissionRequest
方法.
You need custom a WebView
to override the WebChromeClient
's OnPermissionRequest
method.
MyWebView
类:
public class MyWebView: WebView
{
}
MyWebViewRenderer
和MyWebClient
类:
[assembly: ExportRenderer(typeof(App45.MyWebView), typeof(MyWebViewRenderer))]
namespace App45.Droid
{
public class MyWebViewRenderer : WebViewRenderer
{
Activity mContext;
public MyWebViewRenderer(Context context) : base(context)
{
this.mContext = context as Activity;
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
Control.Settings.JavaScriptEnabled = true;
Control.ClearCache(true);
Control.SetWebChromeClient(new MyWebClient(mContext));
}
public class MyWebClient : WebChromeClient
{
Activity mContext;
public MyWebClient(Activity context) {
this.mContext = context;
}
[TargetApi(Value = 21)]
public override void OnPermissionRequest(PermissionRequest request)
{
mContext.RunOnUiThread(() => {
request.Grant(request.GetResources());
});
}
}
}
}
此处,我提供了一个演示供您测试.相机应该可以为您工作.
Here, I have provided a demo for you to test. The camera should work for you.
这篇关于Xamarin WebView上的相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!