Skip to content

Commit fb6e664

Browse files
Fix camera view app crashes when switching camera on windows (#2907)
* Update Sample App + Fix Bug * Add `ICameraProvider.AvailableCamerasChanged` * Update MockCameraProvider * Add Unit Tests * Add Unit Tests
1 parent d772b6c commit fb6e664

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/CameraView/CameraViewPage.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161

6262
<FlexLayout Margin="5" JustifyContent="SpaceBetween" Wrap="Wrap">
6363

64+
<Picker
65+
Title="Cameras"
66+
ItemsSource="{Binding Cameras}"
67+
ItemDisplayBinding="{Binding Name}"
68+
SelectedItem="{Binding SelectedCamera}" />
69+
6470
<Picker
6571
Title="Flash"
6672
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/CameraView/CameraViewViewModel.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,36 @@ partial void OnSelectedResolutionChanged(Size value)
6666
UpdateResolutionText();
6767
}
6868

69+
partial void OnSelectedCameraChanged(CameraInfo? oldValue, CameraInfo? newValue)
70+
{
71+
UpdateCameraInfoText();
72+
}
73+
74+
void UpdateCameraInfoText()
75+
{
76+
if (SelectedCamera is null)
77+
{
78+
CameraNameText = string.Empty;
79+
ZoomRangeText = string.Empty;
80+
}
81+
else
82+
{
83+
CameraNameText = $"{SelectedCamera.Name}";
84+
ZoomRangeText = $"Min Zoom: {SelectedCamera.MinimumZoomFactor}, Max Zoom: {SelectedCamera.MaximumZoomFactor}";
85+
UpdateFlashModeText();
86+
}
87+
}
88+
6989
void UpdateFlashModeText()
7090
{
7191
if (SelectedCamera is null)
7292
{
73-
return;
93+
FlashModeText = string.Empty;
94+
}
95+
else
96+
{
97+
FlashModeText = $"{(SelectedCamera.IsFlashSupported ? $"Flash mode: {FlashMode}" : "Flash not supported")}";
7498
}
75-
FlashModeText = $"{(SelectedCamera.IsFlashSupported ? $"Flash mode: {FlashMode}" : "Flash not supported")}";
7699
}
77100

78101
void UpdateCurrentZoomText()

src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public async partial ValueTask RefreshAvailableCameras(CancellationToken token)
1414
var deviceInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture).AsTask(token);
1515
var mediaFrameSourceGroup = await MediaFrameSourceGroup.FindAllAsync().AsTask(token);
1616
var videoCaptureSourceGroup = mediaFrameSourceGroup.Where(sourceGroup => deviceInfoCollection.Any(deviceInfo => deviceInfo.Id == sourceGroup.Id)).ToList();
17-
var mediaCapture = new MediaCapture();
1817

1918
var availableCameras = new List<CameraInfo>();
2019

2120
foreach (var sourceGroup in videoCaptureSourceGroup)
2221
{
22+
using var mediaCapture = new MediaCapture();
2323
await mediaCapture.InitializeCameraForCameraView(sourceGroup.Id, token);
2424

2525
CameraPosition position = CameraPosition.Unknown;

0 commit comments

Comments
 (0)