title: How to switch between webcams when using CaptureElement/MediaCapture?
body: <p>I am trying to provide an option to switch between webcams used to display preview using CaptureElement/MediaCapture. Unfortunately I tried multiple combinations of call sequences and the preview only shows up for the first device I use.</p>
  
  <p>This is what I have been trying to do:</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;CaptureElement
      x:Name="captureElement"
      Stretch="UniformToFill" /&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>MediaCapture mediaCapture;
  DeviceInformationCollection devices;
  int currentDevice = 0;
  
  private async void LayoutRoot_Tapped(object sender, Windows.UI.Xaml.Input.TappedEventArgs e)
  {
      if (devices != null)
      {
          currentDevice = (currentDevice + 1) % devices.Count;
          InitializeWebCam();
      }
  }
  
  private async void InitializeWebCam()
  {
      if (devices == null)
      {
          devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
          ListDeviceDetails();
      }
  
      if (mediaCapture != null)
      {
          await mediaCapture.StopPreviewAsync();
  
          this.captureElement.Source = null;
      }
  
      mediaCapture = new MediaCapture();
      await mediaCapture.InitializeAsync(
          new MediaCaptureInitializationSettings
          {
              VideoDeviceId = devices[currentDevice].Id
          });
  
      this.captureElement.Source = mediaCapture;
      await mediaCapture.StartPreviewAsync();
  }
  
  private void ListDeviceDetails()
  {
      int i = 0;
  
      foreach (var device in devices)
      {
          Debug.WriteLine("* Device [{0}]", i++);
          Debug.WriteLine("EnclosureLocation.InDock: " + device.EnclosureLocation.InDock);
          Debug.WriteLine("EnclosureLocation.InLid: " + device.EnclosureLocation.InLid);
          Debug.WriteLine("EnclosureLocation.Panel: " + device.EnclosureLocation.Panel);
          Debug.WriteLine("Id: " + device.Id);
          Debug.WriteLine("IsDefault: " + device.IsDefault);
          Debug.WriteLine("IsEnabled: " + device.IsEnabled);
          Debug.WriteLine("Name: " + device.Name);
          Debug.WriteLine("IsDefault: " + device.IsDefault);
  
          foreach (var property in device.Properties)
          {
              Debug.WriteLine(property.Key + ": " + property.Value);
          }
      }
  }
  </code></pre>
  
  <p>It seems like it does work to switch to second camera once in a while (below 10% of times) and then stays all black when I go back to the first one.</p>
  
  <p>Sometimes the app hangs after I try to switch the camera once or twice (it stops responding to input, it is stuck in App.Run(), though the camera preview keeps refreshing).</p>
  
  <p>Other times - it works in a way that it shows preview from the first device, but does not for the other one and when I go back to the first one - it works fine with it again.</p>
  
  <p>Bugs?</p>
  
  <p>There does not seem to be a Dispose or Uninitialize method anywhere. These are the properties I see (it's Samsung's Build 2011 tablet):</p>
  
  <pre><code>* Device [0]
  EnclosureLocation.InDock: False
  EnclosureLocation.InLid: False
  EnclosureLocation.Panel: Front
  Id: \\?\USB#VID_2232&amp;PID_1021&amp;MI_00#7&amp;2469C269&amp;0&amp;0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\GLOBAL
  IsDefault: False
  IsEnabled: True
  Name: WebCam SC-20FHM11347N
  IsDefault: False
  System.ItemNameDisplay: WebCam SC-20FHM11347N
  System.Devices.DeviceInstanceId: USB\VID_2232&amp;PID_1021&amp;MI_00\7&amp;2469C269&amp;0&amp;0000
  System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2068
  System.Devices.InterfaceEnabled: True
  System.Devices.IsDefault: False
  * Device [1]
  EnclosureLocation.InDock: False
  EnclosureLocation.InLid: False
  EnclosureLocation.Panel: Back
  Id: \\?\USB#VID_2232&amp;PID_1022&amp;MI_00#7&amp;27072759&amp;0&amp;0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\GLOBAL
  IsDefault: False
  IsEnabled: True
  Name: WebCam SC-30H2L11449N
  IsDefault: False
  System.ItemNameDisplay: WebCam SC-30H2L11449N
  System.Devices.DeviceInstanceId: USB\VID_2232&amp;PID_1022&amp;MI_00\7&amp;27072759&amp;0&amp;0000
  System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2068
  System.Devices.InterfaceEnabled: True
  System.Devices.IsDefault: False
  </code></pre>