我正在尝试在屏幕上显示当前的相机分辨率,以便用户知道当前的分辨率是多少,并在必要时随意调整。在我的应用程序中,所有相机功能都有效,但由于某种原因,每当我尝试访问当前相机分辨率并将其显示在文本框中时,都会收到 UnauthorizedAccessException。我尝试在 OnNavigatedTo 事件和我的 camera_Initialized 事件中查询分辨率值。我做错了什么,我该如何更改我当前的实现以使其正常工作?
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
}
}
}