title: PhotoCamera issue while Initialization
body: <p>I am developing windows phone 8 app which use <code>PhotoCamera</code> for capturing image. I am getting issue when I press screen lock key. My <code>PhotoCamera</code> object not disposing when I press lock key. I am getting the problem when I press <strong>screen lock</strong> key at the time of <code>Camera Initialization</code>. </p>
  
  <p>Here is some code.</p>
  
  <p><strong>Button click code</strong> </p>
  
  <pre><code>_photoCamera = new PhotoCamera();
  _photoCamera.Initialized += OnPhotoCameraInitialized;
  _photoCamera.CaptureImageAvailable += new EventHandler&lt;Microsoft.Devices.ContentReadyEventArgs&gt;(cam_CaptureImageAvailable);
  _photoCamera.AutoFocusCompleted += new EventHandler&lt;CameraOperationCompletedEventArgs&gt;(cam_AutoFocusCompleted);
  
  _previewTransform.Rotation = _photoCamera.Orientation;
  _previewVideo.SetSource(_photoCamera);
  </code></pre>
  
  <p>Here is my <strong>OnPhotoCameraInitialized</strong> code</p>
  
  <pre><code> private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
   {
          try
          {
              Dispatcher.BeginInvoke(() =&gt;
              {
                  gvCamera.Visibility = Visibility.Visible;
                  gvCameraImage.Visibility = Visibility.Collapsed;
                  Cancel.Visibility = Visibility.Visible;
                  imgScanCancle.Visibility = Visibility.Visible;
              });
              cameraInit = true;
              _photoCamera.FlashMode = FlashMode.Auto;
              _photoCamera.Focus();
          }
          catch (Exception ex)
          {
              //MessageBox.Show(ex.Message);
          }
   }
  </code></pre>
  
  <p>Here is my <strong>NavigationFrom</strong> code</p>
  
  <pre><code>protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  {
          base.OnNavigatedFrom(e);
          try
          {
              if (cameraInit)
              {
                  if (_photoCamera != null)
                  {
                      _photoCamera.Dispose();
                      _photoCamera.Initialized -= OnPhotoCameraInitialized;
                      _photoCamera.CaptureImageAvailable -= cam_CaptureImageAvailable;
                      _photoCamera.AutoFocusCompleted -= cam_AutoFocusCompleted;
                      _photoCamera = null;
                      cameraInit = false;
                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
   }
  </code></pre>
  
  <p>I have re-Initialized photocamera in <strong>OnNavigatedTo</strong> Method</p>
  
  <p>Here is <strong>OnNavigatedTo</strong> code.</p>
  
  <pre><code>            if (GlobalSettings.IspreservedState)
              {
                  if (!GlobalSettings.istest_performed)
                  {                        
                      if (_photoCamera == null)
                      {
                          _nbTry = 0;
  
                          _photoCamera = new PhotoCamera();
                          _photoCamera.Initialized += OnPhotoCameraInitialized;
                          _photoCamera.CaptureImageAvailable += new EventHandler&lt;Microsoft.Devices.ContentReadyEventArgs&gt;(cam_CaptureImageAvailable);
                          _photoCamera.AutoFocusCompleted += new EventHandler&lt;CameraOperationCompletedEventArgs&gt;(cam_AutoFocusCompleted);
  
                          _previewTransform.Rotation = _photoCamera.Orientation;
                          _previewVideo.SetSource(_photoCamera);
                      }
                  }
              }
  </code></pre>
  
  <p>Basically what I am trying to do is Capture the picture and display it. I have a problem when I press a lock key before camera Initialization. When I press lock key, OnNavigatedFrom code start executing. When it reach to <code>if(cameraInit)</code> inside code not executed because <code>cameraInit</code> not true. I have set <code>cameraInit = true;</code> in  <strong>OnPhotoCameraInitialized</strong> method. But unfortunately this code block not execute. so my question is How can I dispose camera object? I can dispose it only when it is fully Initialized. But I press lock key before <code>Initialization</code>. Is there any way to clean the video brush?</p>