[BUG] CameraView System.InvalidOperationException: Unable to retrieve Handler
Is there an existing issue for this?
- [x] I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
When I have a CameraView and start a Preview so a Picture can be captured the System.InvalidOperationException: Unable to retrieve Handler is raised.
Occurs on both iOS and Android.
{System.InvalidOperationException: Unable to retrieve Handler at CommunityToolkit.Maui.Views.CameraView.get_Handler() in /_/src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs:line 195 at CommunityToolkit.Maui.Views.CameraView.StartCameraPreview(CancellationToken token) in /_/src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs:line 255 at MauiAppCameraView.Pages.CameraViewPage.OnAppearing() in D:\repo\MauiAppCameraView\MauiAppCameraView\Pages\CameraViewPage.xaml.cs:line 18 at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state) at Foundation.NSAsyncSynchronizationContextDispatcher.Apply() in /Users/builder/azdo/_work/2/s/macios/src/Foundation/NSAction.cs:line 179 at Foundation.NSAsyncSynchronizationContextDispatcher.__Registrar_Callbacks__.callback_3681_Foundation_NSAsyncSynchronizationContextDispatcher_Apply(IntPtr pobj, IntPtr sel, IntPtr* exception_gchandle)}
Expected Behavior
CameaView works as documented. start a Preview and then capture an image.
Steps To Reproduce
- Open and debug simple app from repo at https://github.com/Chris-Marassovich/MauiAppCameraView
- From the Shell Menu choose the "Camera" menu option and you should see the exception.
Link to public reproduction project repository
https://github.com/Chris-Marassovich/MauiAppCameraView
Environment
"CommunityToolkit.Maui.Camera" Version="3.0.2"
"Microsoft.Maui.Controls" Version="9.0.120"
"CommunityToolkit.Mvvm" Version="8.4.0"
"CommunityToolkit.Maui" Version="12.2.0"
.NET workload is at
dotnet workload --info
Workload version: 9.0.302
Configured to use workload sets when installing new manifests.
Android is using an emulator Pixel 4 Android 12.0 API 31
iOS iPhone 13 Pro iOs 17.7
Anything else?
Temporary workaround
Falling back to MediaPicker.CapturePhotoAsync() resolves the issue, but this is not suitable for us as I need to default the flash to off. This is a critical point for us and the only way I can programmatically default the flash to off is to use the CameraView.
If I can use the MediaPicker and still be able to default the flash to off that would be extremely helpful.
no need to call await Camera.StartCameraPreview(cancellationTokenSource.Token); on Appearing
@VladislavAntonyuk thank you for responding and you are exactly right. Just navigating to a page that has the CameraView control on it will automatically start the preview.
I was following the doco and the sample app and simple assumed I needed to call it to start the preview.
Maybe the doco could be updated to inform people that the control will automatically start preview and when you should use the StartCameraPreview method and matching command?
cheers Chris ...