[Proposal] Improve CameraView camera selection and preview startup behavior
#3250
zhitaop
started this conversation in
New Feature Discussions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Background
CameraView.SelectedCamerais currently a bindable property that can be set by developers.It currently serves two purposes:
CameraViewis running.CameraViewshould start with.When
SelectedCamerais set and the camera preview is already initialized/running, the preview is stopped and restarted with the newly selected camera.Separately,
CameraViewalso automatically starts the preview when loaded. If no camera is selected at startup, theSelectedCamerais set to the first available camera by default.Issue
Because camera selection and preview startup are both automatic, preview startup can be triggered through multiple paths, and the order of those paths is not deterministic.
For example, consider a developer binding
SelectedCamerato a view model property because they want theCameraViewto start with a specific camera. Depending on timing, either of the following can happen:Outcome 1: Correct camera, but unnecessary restart
CameraViewstarts automatically.SelectedCamerabinding has not been applied yet, so the first available camera is used.SelectedCamerabinding is applied.Issue: the final camera is correct, but the preview starts twice unnecessarily.
Outcome 2: State mismatch
CameraViewstarts automatically.SelectedCamerabinding has not been applied yet, so the first available camera is used.SelectedCamerabinding is applied, while the initial automatic startup is still in progress.Issue:
SelectedCamerais different from the camera actually used by the preview.Root cause
The current design couples three concepts too tightly:
SelectedCamerais both an input and a reflection of state, while setting it can also trigger a camera restart. Combined with automatic startup, this creates timing-dependent behavior.Proposal 1: Make active camera state read-only and make camera selection explicit
1. Make
SelectedCameraread-onlyChange
SelectedCameraso that it is read-only and reflects the actual current active camera.2. (Optional) Rename
SelectedCameraThe name
SelectedCameramay be ambiguous because it can still imply “the developer’s desired camera.”A clearer name may be:
ActiveCameraorCurrentCamera3. Make camera selection an explicit operation through
StartCameraPreview(...)Add an overload that allows developers to explicitly start the preview with a specific camera:
Suggested behavior:
cameraInfois provided, start the preview with that camera.cameraInfoisnull, use the current active camera or the default first available camera.SelectedCamera/ActiveCamera.4. Add
AutoStartAdd an
AutoStartproperty which defaults to true:Suggested behavior:
AutoStart = true: preserve current behavior - automatically start the camera preview using the first available camera.AutoStart = false: do not start automatically; developers need to explicitly callStartCameraPreview(cameraInfo).This allows developers to choose the initial camera explicitly, while avoids unnecessary restarts.
Pros
AutoStart = truepreserves the existing default behavior.Cons
SelectedCamerais made read-only. Existing XAML bindings toSelectedCameraas an input would need to change.SelectedCamerato switch cameras would need to use the new API.Proposal 2: Keep
SelectedCamerasettable, but remove automatic startup/default camera selectionSummary
Keep
SelectedCameraas a bindable property.Remove the behavior where
CameraViewautomatically starts the preview and selects the first available camera.Developers would need to explicitly:
SelectedCamera.StartCameraPreview().If
SelectedCamerachanges while preview is running, the preview switches camera.Pros
SelectedCameraremains bindable and settable.SelectedCameracan remain.Cons
StartCameraPreview()in all cases.CameraViewon a page and have it start.SelectedCameraremains a property change with a native lifecycle side effect.Personally, I prefer proposal 1. Although it breaks existing API and requires more change from developer, it provides a clearer long-term API design. Feedback and alternative suggestions are also welcome!
All reactions