Skip to content

Refactor CameraView.CaptureImage, CameraView.StartCameraPreview and CameraView.StopCameraPreview #2655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

TheCodeTraveler
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler commented May 6, 2025

Description of Change

This PR refactors the following public methods in CameraView:

  • CameraView.CaptureImage(CancellationToken),
  • CameraView.StartCameraPreview(CancellationToken)
  • CameraView.StopCameraPreview()

Previously, these methods deferred to the Handler to invoke the underlying platform-specfic implemetnation via Handler.Invoke(). The problem with this approach is that that Handler.Invoke is not await'able, yet was invoking asynchronous methods using async void.

Using async void lead to issues where developers are unable to catch valid Exceptions thrown by the Operating System, as noted in #2520.

With this PR, developers will be able to wrap these methods, like await CaptureImage(token), in a try/catch block to gracefully handle any exception thrown.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Additional information

In #2520 we discussed promoting the underlying CameraManager class to be public. However, after further investigation, because each CameraView requires a unique instance of CameraManager, it is best to keep CameraManager internal.

This PR also demotes CameraView.OnMediaCaptured and CameraView.OnMediaCapturedFailed from public to internal. These methods fire event MediaCaptured and event MediaCaptureFailed, respectively, and we should not allow library users to manually fire these events.

@TheCodeTraveler TheCodeTraveler added the breaking change This label is used for PRs that include a breaking change label May 6, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the CameraView methods to remove the non-awaitable Handler.Invoke pattern and instead use properly awaitable asynchronous methods from CameraManager. Additionally, it removes the IAsynchronousHandler interface and updates namespaces and command mappings to align with the new design.

  • Refactored CameraView public methods (CaptureImage, StartCameraPreview, StopCameraPreview) to directly call asynchronous CameraManager methods.
  • Removed the asynchronous handler pattern and updated command mappings in CameraViewHandler.
  • Updated namespace usage by removing references to CommunityToolkit.Maui.Core.Primitives.

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs Refactored async method implementations and updated error messages.
src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.windows.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.macios.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.android.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/Primitives/MediaCapturedEventArgs.shared.cs Updated namespace from Views to Core.
src/CommunityToolkit.Maui.Camera/Primitives/MediaCaptureFailedEventArgs.shared.cs Updated namespace from Views to Core.
src/CommunityToolkit.Maui.Camera/Primitives/CameraViewDefaults.shared.cs Updated namespace and removed unused using directive.
src/CommunityToolkit.Maui.Camera/Primitives/CameraPosition.shared.cs Updated namespace from Primitives to Core.
src/CommunityToolkit.Maui.Camera/Primitives/CameraFlashMode.shared.cs Updated namespace from Primitives to Core.
src/CommunityToolkit.Maui.Camera/Interfaces/ICameraView.shared.cs Removed IAsynchronousHandler inheritance and updated method signatures.
src/CommunityToolkit.Maui.Camera/Interfaces/IAsynchronouseHandler.cs Removed the asynchronous handler interface.
src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs Updated command mappings and refactored async command implementations.
src/CommunityToolkit.Maui.Camera/Extensions/CameraViewExtensions.macios.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/Extensions/CameraViewExtensions.android.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/CameraManager.windows.cs Removed unused namespace reference and updated code accordingly.
src/CommunityToolkit.Maui.Camera/CameraManager.shared.cs Updated namespace from Core.Primitives to Core.
src/CommunityToolkit.Maui.Camera/CameraManager.net.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/CameraManager.macios.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/CameraManager.android.cs Removed unused namespace reference.
src/CommunityToolkit.Maui.Camera/CameraInfo.shared.cs Removed unused namespace reference.

@TheCodeTraveler TheCodeTraveler added the Camera 📸 Issue/PR associated with the Camera package label May 6, 2025
@TheCodeTraveler TheCodeTraveler added the pending documentation This feature requires documentation label May 6, 2025
@TheCodeTraveler TheCodeTraveler requested a review from Copilot May 6, 2025 19:57
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors core CameraView methods to support proper asynchronous operations and exception handling by removing the async void patterns, updating method signatures, and adjusting internal namespaces.

  • Refactors asynchronous CameraView methods (CaptureImage, StartCameraPreview, and StopCameraPreview)
  • Removes obsolete IAsynchronousHandler interface and updates namespace references
  • Updates command mappings in CameraViewHandler to use proper asynchronous calls

Reviewed Changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/CommunityToolkit.Maui.Camera/Providers/* Removed redundant using directives and updated namespaces
src/CommunityToolkit.Maui.Camera/Primitives/* Changed namespaces from Views to Core
src/CommunityToolkit.Maui.Camera/Interfaces/* Updated method signatures and access modifiers
src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs Updated command mappings and refactored async methods
src/CommunityToolkit.Maui.Camera/CameraManager.* Removed obsolete using directives and updated namespaces
samples/CommunityToolkit.Maui.Sample/Pages/Views/CameraView/CameraViewPage.xaml.cs Updated namespace usage
Comments suppressed due to low confidence (1)

src/CommunityToolkit.Maui.Camera/Handlers/CameraViewHandler.shared.cs:146

  • The MapStopCameraPreview function uses await inside its body but is not marked as async, which will lead to a compile error. Consider adding the async modifier to the function signature.
static void MapStopCameraPreview(CameraViewHandler handler, ICameraView view, object? arg3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change This label is used for PRs that include a breaking change Camera 📸 Issue/PR associated with the Camera package pending documentation This feature requires documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] CameraView on Windows crashes app when the camera is in use
1 participant