我试图在屏幕上显示当前的相机分辨率,以便用户知道当前的分辨率,并在必要时随时调整。在我的应用程序中,所有相机功能都有效,但出于某种原因,每当我尝试访问当前相机分辨率并将其显示在文本框中时,我都会收到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
}
}
}
答案 0 :(得分:1)
由于您尝试在UI上显示某些内容,因此需要使用Dispatcher来执行此操作。
Deployment.Current.Dispatcher.BeginInvoke( ()=> { resolutionValueTextBlock.Text = "resolution: " + camera.Resolution.ToString(); });