01Nov
How to detect if camera is available in Windows Store apps?
Posted by admin as .Net, C#, Microsoft, Windows 8, Windows Store
If you use CameraCaptureUI, the user will be prompted for permission automatically. But the DeviceInformation API does not indicate whether or not the user has granted permission.
Here is sample class that you can use to see whether a camera is available, with support for data binding
using Common;
using System.Collections.Generic;
using System.Linq;
using Windows.Devices.Enumeration;
namespace Helpers
{
public class VideoCaptureStatus : BindableBase
{
private DeviceWatcher deviceWatcher;
private List<DeviceInformation> videoDevices = new List<DeviceInformation>();
private bool isAvailable;
public VideoCaptureStatus()
{
this.deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.VideoCapture);
this.deviceWatcher.Added += this.DeviceWatcher_Added;
this.deviceWatcher.Removed += this.DeviceWatcher_Removed;
this.deviceWatcher.Updated += this.DeviceWatcher_Updated;
this.deviceWatcher.Start();
}
public bool IsAvailable
{
get { return this.isAvailable; }
private set { this.SetProperty(ref this.isAvailable, value); }
}
private void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInformation)
{
this.videoDevices.Add(deviceInformation);
this.UpdateAvailability();
}
private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInformationUpdate)
{
this.videoDevices.RemoveAll(deviceInformation => deviceInformation.Id == deviceInformationUpdate.Id);
this.UpdateAvailability();
}
private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInformationUpdate)
{
foreach (DeviceInformation deviceInformation in this.videoDevices)
{
if (deviceInformation.Id == deviceInformationUpdate.Id)
{
deviceInformation.Update(deviceInformationUpdate);
}
}
this.UpdateAvailability();
}
private void UpdateAvailability()
{
this.IsAvailable = this.videoDevices.Any(deviceInformation => deviceInformation.IsEnabled);
}
}
}
- CTRL + F5
- Tags: C#, Camera, Metro, Windows 8, Windows Store

2 Responses to How to detect if camera is available in Windows Store apps?
Megan Meyers
February 17th, 2013 at 12:50 pm
With Windows 8 app store, there are a lot of ways in which you can plan on monetizing your creation. This particular post focusses on the In-app advertising model, where you place an advertisement in your game and generate revenue from impressions and click through actions. Let’s take a look how you can add advertisement to your Construct 2 exported game, with an option to show and hide the advertisement as required. In particular, this post is focusing on utilizing the ads from Microsoft Advertising SDK for Windows 8 .
LV bags
April 9th, 2013 at 8:50 pm
http://www.awebcafe.com/blogs/viewstory/1075133