diff --git a/WinUIGallery/ControlPages/CaptureElementPreviewPage.xaml.cs b/WinUIGallery/ControlPages/CaptureElementPreviewPage.xaml.cs index 2a95669d..3ef43a80 100644 --- a/WinUIGallery/ControlPages/CaptureElementPreviewPage.xaml.cs +++ b/WinUIGallery/ControlPages/CaptureElementPreviewPage.xaml.cs @@ -19,6 +19,7 @@ using Windows.Storage.Streams; using System.ComponentModel; using WinUIGallery.Helper; +using System.Threading.Tasks; namespace WinUIGallery.ControlPages { @@ -38,6 +39,17 @@ public CaptureElementPreviewPage() captureContainer.Children.Remove(sv); captureContainer.Children.Add(expandToFillContainer); expandToFillContainer.Children.Add(sv); + + this.Unloaded += this.CaptureElementPreviewPage_Unloaded; + } + + private void CaptureElementPreviewPage_Unloaded(object sender, RoutedEventArgs e) + { + // Needs to run as task to unblock UI thread + if (mediaCapture != null) + { + new Task(mediaCapture.Dispose).Start(); + } } private MediaFrameSourceGroup mediaFrameSourceGroup;