Hello,
I tested Xamarin with reference to this official tutorial on Android API level 30, and the results showed that BindToLifecycle
works as expected and the IsBound
method returns true.
Are you missing permissions or API levels when writing code?
Please refer to the following tutorial and xamarin code sample.
private void StartCamera()
{
var cameraProviderFuture = ProcessCameraProvider.GetInstance(this);
cameraProviderFuture.AddListener(new Runnable(() =>
{
// Used to bind the lifecycle of cameras to the lifecycle owner
var cameraProvider = (ProcessCameraProvider)cameraProviderFuture.Get();
var preview = new Preview.Builder().Build();
preview.SetSurfaceProvider(viewFinder.CreateSurfaceProvider());
this.imageCapture = new ImageCapture.Builder().Build();
var imageAnalyzer = new ImageAnalysis.Builder().Build();
imageAnalyzer.SetAnalyzer(cameraExecutor, new LuminosityAnalyzer(luma =>
Log.Debug(TAG, $"Average luminosity: {luma}")
));
// Select back camera as a default
CameraSelector cameraSelector = null;
if (cameraProvider.HasCamera(CameraSelector.DefaultBackCamera) == true)
cameraSelector = CameraSelector.DefaultBackCamera;
else if (cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera) == true)
cameraSelector = CameraSelector.DefaultFrontCamera;
else
throw new System.Exception("Camera not found");
try
{
// Unbind use cases before rebinding
cameraProvider.UnbindAll();
// Bind use cases to camera
cameraProvider.BindToLifecycle(this, cameraSelector, preview, imageCapture, imageAnalyzer);
}
catch (Exception exc)
{
Log.Debug(TAG, "Use case binding failed", exc);
Toast.MakeText(this, $"Use case binding failed: {exc.Message}", ToastLength.Short).Show();
}
var b = cameraProvider.IsBound(preview); // the result will be true.
}), ContextCompat.GetMainExecutor(this));
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.