2012-09-04 54 views
0

我正試圖在屏幕上顯示當前相機分辨率,以便用戶知道當前分辨率是什麼,如有必要可隨時調整。在我的應用程序中,所有相機功能都可以工作,但由於某種原因,每當我嘗試訪問當前相機分辨率並將其顯示在文本框中時,我都會遇到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 
      }      
     } 
    } 

回答

1

既然你試圖顯示在UI上的東西,你需要使用調度程序這樣做。

Deployment.Current.Dispatcher.BeginInvoke(()=> { resolutionValueTextBlock.Text = "resolution: " + camera.Resolution.ToString(); });