Issue
I have a Xamarin App.
Tha app has a webview control to open a web site. I would like to use the camera of the mobile phone inside a specific web page that needs the camera. If this specific page is not opened, there is no need to request camera permission for the app. So what I'm trying to do, is to send a request from the page javascript to Xamarin app and request the camera access.
I followed this tutorial to create a hybrid webview that allows me to send commands from Javascript to C# : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
So I registered an action that will be called from Javascript as follows :
hybridWebView.RegisterAction(async data =>
{
DisplayAlert("Hi ! you called this action from Javascript.");
// Request camera permission
RequestCameraAccessAsync();
});
With RequestCameraAccessAsync as :
private async void RequestCameraAccessAsync()
{
var status = await Permissions.CheckStatusAsync<Permissions.Camera>();
if (status == PermissionStatus.Granted)
return;
if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
{
// Prompt the user to turn on in settings
// On iOS once a permission has been denied it may not be requested again from the application
return;
}
status = Permissions.RequestAsync<Permissions.Camera>().Result;
}
The bridge between Javascript and C# is working fine. However, whenever the code hits Request Permission it fails saying : Permission request must be invoked on main thread.
I understand this error. But given the fact that the permission request should be done only on a registered action, is there anyway to make this work please ?
Thanks.
Solution
use Essentials MainThread
MainThread.BeginInvokeOnMainThread(() =>
{
// Code to run on the main thread
});
Answered By - Jason
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.