Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

How and When To Set the FlashMode

For an app I’m writing at the moment, I needed to use the camera within the app, but I needed it to be able to focus without the flash turning on every time. I was using the PhotoCamera object within my app and I figured I could just set the FlashMode to be FlashMode.Off. The problem came when my View loaded up, it would throw an exception, every, single, time. Which was starting to get a bit annoying, so what to do?

The Solution

A tweet from @Jaxbox pointed me in the right direction on how to fix this. You basically need to set this property after the Camera has finished initializing.

So basically:

            // Set the photoCamera up
            Loaded += (sender, args) =>
                          {
                              if (photoCamera == null)
                              {
                                  photoCamera = new PhotoCamera();
                                  photoCamera.Initialized += OnPhotoCameraInitialized;
                                  previewVideo.SetSource(photoCamera);

                                  CameraButtons.ShutterKeyHalfPressed += (o, arg) => FocusTheCamera();
                              }
                          };

        // Here is where we set the FlashMode
        private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
        {
            photoCamera.FlashMode = FlashMode.Off;
        }

And that’s it. Now the app doesn’t have an exception thrown and I get the focus being done without the flash.

SL

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: