Await Task.Run()

in templates/Uwp/_comp/_shared/Page.Camera.AddResource._VB/Controls/CameraControl.xaml.vb [191:286]


        Await Task.Run(Async Function()
                           Await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, Async Sub()
                                    Await CleanupCameraAsync()
                                End Sub)
                    End Function)
      End Sub

      Private Async Function StartPreviewAsync() As Task
          PreviewControl.Source = _mediaCapture
          PreviewControl.FlowDirection = If(_mirroringPreview, FlowDirection.RightToLeft, FlowDirection.LeftToRight)

          If _mediaCapture IsNot Nothing Then
              Await _mediaCapture.StartPreviewAsync()
              Await SetPreviewRotationAsync()
              _isPreviewing = True
          End If
      End Function

      Private Async Function SetPreviewRotationAsync() As Task
          _displayOrientation = _displayInformation.CurrentOrientation
          Dim rotationDegrees As Integer = _displayOrientation.ToDegrees()

          If _mirroringPreview Then
              rotationDegrees = (360 - rotationDegrees) Mod 360
          End If

          If _mediaCapture IsNot Nothing Then
              Dim props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview)
              props.Properties.Add(_rotationKey, rotationDegrees)
              Await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, Nothing)
          End If
      End Function

      Private Async Function StopPreviewAsync() As Task
          _isPreviewing = False
          Await _mediaCapture.StopPreviewAsync()
          PreviewControl.Source = Nothing
      End Function

      Private Async Function ReencodeAndSavePhotoAsync(stream As IRandomAccessStream, photoOrientation As PhotoOrientation) As Task(Of String)
          Using inputStream = stream
              Dim decoder = Await BitmapDecoder.CreateAsync(inputStream)

              Dim file = Await ApplicationData.Current.LocalFolder.CreateFileAsync("photo.jpeg", CreationCollisionOption.GenerateUniqueName)

              Using outputStream = Await file.OpenAsync(FileAccessMode.ReadWrite)
                  Dim encoder = Await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder)

                  Dim properties = New BitmapPropertySet() From {
                    {"System.Photo.Orientation", New BitmapTypedValue(photoOrientation, PropertyType.UInt16)}
                  }

                  Await encoder.BitmapProperties.SetPropertiesAsync(properties)
                  Await encoder.FlushAsync()
              End Using

              Return file.Path
          End Using
      End Function

      Private Sub RegisterOrientationEventHandlers()
          If _orientationSensor IsNot Nothing Then
              AddHandler _orientationSensor.OrientationChanged, AddressOf OrientationSensor_OrientationChanged
              _deviceOrientation = _orientationSensor.GetCurrentOrientation()
          End If

          AddHandler _displayInformation.OrientationChanged, AddressOf DisplayInformation_OrientationChanged
          _displayOrientation = _displayInformation.CurrentOrientation
      End Sub

      Private Sub UnregisterOrientationEventHandlers()
          If _orientationSensor IsNot Nothing Then
              RemoveHandler _orientationSensor.OrientationChanged, AddressOf OrientationSensor_OrientationChanged
          End If

          RemoveHandler _displayInformation.OrientationChanged, AddressOf DisplayInformation_OrientationChanged
      End Sub

      Private Sub OrientationSensor_OrientationChanged(sender As SimpleOrientationSensor, args As SimpleOrientationSensorOrientationChangedEventArgs)
          If args.Orientation <> SimpleOrientation.Faceup AndAlso args.Orientation <> SimpleOrientation.Facedown Then
              _deviceOrientation = args.Orientation
          End If
      End Sub

      Private Async Sub DisplayInformation_OrientationChanged(sender As DisplayInformation, args As Object)
          _displayOrientation = sender.CurrentOrientation
          Await SetPreviewRotationAsync()
      End Sub

      Private Shared Sub OnPanelChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
          Dim ctrl = DirectCast(d, CameraControl)

          If ctrl.IsInitialized Then
              ctrl.CleanAndInitialize()
          End If
        End Sub