Reputation: 4056
I am attempting to display the current camera resolution on the screen, so that users will know what the current resolution is, and feel free to adjust it if necessary. In my application, all camera functionality works, but for some reason whenever I try to access the current camera resolution and display it in a textbox, I get an UnauthorizedAccessException. I have tried querying the resolution value in the OnNavigatedTo event and my camera_Initialized event. What am I doing wrong, and how might I change my current implementation for this to work?
MainPage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||
(PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))
{
// Initialize the camera, when available.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
// Use front-facing camera if available.
camera = new PhotoCamera(CameraType.Primary);
camera.Initialized += camera_Initialized;
viewfinderBrush.SetSource(camera);
//display current resolution
resolutionValueTextBlock.Text = "resolution: " + camera.Resolution.ToString(); //UnauthorizedAccessException occurs here
}
}
...
}
void camera_Initialized(object sender, CameraOperationCompletedEventArgs e)
{
if (e.Succeeded)
{
if (Settings.firstRun.Value)
{
//Set highest camera resolution on first run and by default
Size resolution = camera.AvailableResolutions.OrderByDescending(s => s.Width).First();
camera.Resolution = resolution;
//display current resolution
resolutionValueTextBlock.Text = "resolution: " + camera.Resolution.ToString(); //UnauthorizedAccessException
...
}
else
{
Size resolution = camera.AvailableResolutions.ElementAt(Settings.tempResolutionIndex.Value);
camera.Resolution = resolution;
//display current resolution
resolutionValueTextBlock.Text = "resolution: " + camera.Resolution.ToString(); //UnauthorizedAccessException
}
}
}
Upvotes: 0
Views: 452
Reputation: 4268
Since you are trying to display something on the UI, you need to use the Dispatcher to do so.
Deployment.Current.Dispatcher.BeginInvoke( ()=> { resolutionValueTextBlock.Text = "resolution: " + camera.Resolution.ToString(); });
Upvotes: 1