2015-07-12 16 views
0
重置頁面

我有一個攝像頭下面的代碼:如何在WPF

public sealed partial class Camera : Page 
{ 
    double width, height; 
    MediaCapture captureManager; 
    bool flashOn = false; 
    public Camera() 
    { 
     this.InitializeComponent(); 
    } 

    /// <summary> 
    /// Invoked when this page is about to be displayed in a Frame. 
    /// </summary> 
    /// <param name="e">Event data that describes how this page was reached. 
    /// This parameter is typically used to configure the page.</param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     captureManager = new MediaCapture(); 
     imagePreview.Source = null; 
     capturePreview.Source = null; 
     DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; 
     width = Convert.ToInt32(Window.Current.Bounds.Width); 
     height = Convert.ToInt32(Window.Current.Bounds.Height); 
     capturePreview.Width = width; 
     capturePreview.Height = height; 
     imagePreview.Width = width; 
     imagePreview.Height = height; 
     Starter(); 

    } 
    async private void Starter() 
    { 
     await captureManager.InitializeAsync(); 
     StartCapturePreview_Click(); 
    } 

    async private void StartCapturePreview_Click() 
    { 
     capturePreview.Source = captureManager; 
     await captureManager.StartPreviewAsync(); 
    } 

    async private void StopCapturePreview_Click(object sender, RoutedEventArgs e) 
    { 
     await captureManager.StopPreviewAsync(); 
    } 

    private async void focus_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      while (true) 
      { 
       await captureManager.VideoDeviceController.FocusControl.UnlockAsync(); 
       var focusSettings = new FocusSettings(); 
       focusSettings.AutoFocusRange = AutoFocusRange.Normal; 
       focusSettings.Mode = FocusMode.Auto; 
       focusSettings.WaitForFocus = true; 
       focusSettings.DisableDriverFallback = false; 
       captureManager.VideoDeviceController.FocusControl.Configure(focusSettings); 
       await captureManager.VideoDeviceController.FocusControl.FocusAsync(); 
      } 
     } 
     catch { } 
    } 

    private async void flash_Click(object sender, RoutedEventArgs e) 
    { 
     if (flashOn) flashOn = false; 
     else flashOn = true; 
     CapturePhoto.Content = flashOn.ToString(); 
     var foc = captureManager.VideoDeviceController.FocusControl; 
     await foc.FocusAsync(); 
    } 

    async private void CapturePhoto_Click(object sender, RoutedEventArgs e) 
    { 
     var flash = captureManager.VideoDeviceController.TorchControl; 
     if (flashOn) flash.Enabled = true; 
     await Task.Delay(500); 
     ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); 

     // create storage file in local app storage 
     StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
      "Photo.jpg", 
      CreationCollisionOption.ReplaceExisting); 

     // take photo 
     await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file); 

     // Get photo as a BitmapImage 
     BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); 

     // imagePreivew is a <Image> object defined in XAML 
     imagePreview.Source = bmpImage; 
     flash.Enabled = false; 
     Frame.Navigate(typeof(MainPage)); 
    } 
} 

我試圖找到一種方法來重新設置頁面(例如添加一個按鈕,重拍照片或返回但是找不到方法... 我試着改變OnNavigatedTo函數中所有對象的初始構造,但它似乎仍然不起作用。

回答

0

在移動頁面並在OnNavigatedTo()中創建它之前,只需刪除captureManager並重置它即可。

0

重置/重新加載跳到我的腦海頁2層最簡單的方法是:

編寫重置所有字段和屬性爲其默認狀態的方法,然後調用從按鈕按下此方法。

或者:

啓動,需要從另一個控制復位並連接復位按鈕,在父控件的方法來關閉,然後重新打開網頁窗口。

希望有幫助。

+0

第一種方法不起作用。 我並沒有完全理解你的第二種解決方案 – IsaacDj