Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace CommunityToolkit.Maui.Core.Handlers;
/// </summary>
public partial class CameraViewHandler : ViewHandler<ICameraView, NativePlatformCameraPreviewView>, IDisposable
{
CancellationTokenSource? cts;
bool isDisposed;

/// <summary>
/// The currently defined mappings between properties on the <see cref="ICameraView"/> and
/// properties on the <see cref="NativePlatformCameraPreviewView"/>.
Expand Down Expand Up @@ -82,9 +85,24 @@ void Init(ICameraView view)
/// <inheritdoc/>
protected override async void ConnectHandler(NativePlatformCameraPreviewView platformView)
{
base.ConnectHandler(platformView);

await CameraManager.ConnectCamera(CancellationToken.None);
cts?.Cancel();
cts?.Dispose();
Comment thread
zhitaop marked this conversation as resolved.
Outdated
cts = new();
try
{
base.ConnectHandler(platformView);
await CameraManager.ConnectCamera(cts.Token);
}
catch (OperationCanceledException) when (cts.IsCancellationRequested)
{
// Catch and ignore the OperationCanceledException if it was caused by our own cancellation token,
// e.g. the handler is disconnected before the camera connection process completes.
}
Comment thread
zhitaop marked this conversation as resolved.
catch (Exception)
{
DisconnectHandler(platformView);
throw;
Comment thread
TheCodeTraveler marked this conversation as resolved.
Outdated
}
}

/// <inheritdoc/>
Expand All @@ -103,11 +121,21 @@ protected override void DisconnectHandler(NativePlatformCameraPreviewView platfo
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}
if (disposing)
{
cts?.Cancel();
cts?.Dispose();
Comment thread
zhitaop marked this conversation as resolved.
Outdated
cts = null;

cameraManager?.Dispose();
cameraManager = null;
}

isDisposed = true;
}

#if WINDOWS
Expand Down
Loading