2017-11-02 9 views
0

現在、私はUWPアプリケーションを開発しており、私のプログラムにはカメラ機能があります。しかし、私はその機能のタイマーを実装したいと思っています。UWPのカメラ機能のタイマー

私は、側で好みのタイミングを選択し、 "写真を撮る"ボタンをクリックし、カメラの画面上でタイマーを表示し、オンクリックの下のイベントを "take a 「画像」ボタンは、ユーザの選択に応じて遅延される。ここで

camera page

の.xamlからの私のコードです:xaml.csから

<CaptureElement Name="PreviewControl" Margin="566,77,166,50"/> 
<Button x:Name="PhotoButton" Content="Take a picture!" HorizontalAlignment="Left" Margin="990,678,0,-91" VerticalAlignment="Top" Click="PhotoButton_Click" Height="45" Width="313" Background="White" Foreground="Black"/> 

//Timer buttons 
<Button x:Name="Timer_3sec" Content="3 seconds" HorizontalAlignment="Left" Margin="138,125,0,0" VerticalAlignment="Top" Height="66" Width="262" Background="White" Foreground="Black"/> 
<Button x:Name="Timer_5sec" Content="5 seconds" HorizontalAlignment="Left" Margin="138,234,0,0" VerticalAlignment="Top" Height="66" Width="262" Background="White" Foreground="Black"/> 
<Button x:Name="Timer_7sec" Content="7 seconds" HorizontalAlignment="Left" Margin="138,337,0,0" VerticalAlignment="Top" Height="66" Width="262" Background="White" Foreground="Black"/> 

コード:

private async void PhotoButton_Click(object sender, RoutedEventArgs e) 
    { 
     await TakePhotoAsync(); 
    } 

private async Task TakePhotoAsync() 
    { 

     var stream = new InMemoryRandomAccessStream(); 

     Debug.WriteLine("Taking photo..."); 
     await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); 

     try 
     { 
      var file = await _captureFolder.CreateFileAsync("SimplePhoto.jpg", CreationCollisionOption.GenerateUniqueName); 
      Debug.WriteLine("Photo taken! Saving to " + file.Path); 

      var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation()); 

      await ReencodeAndSavePhotoAsync(stream, file, photoOrientation); 
      Debug.WriteLine("Photo saved!"); 

      await Helpers.MessageDialogHelpers.ShowNoActionMessageBox("Your photo has been taken!", ""); 


     } 
     catch (Exception ex) 
     { 
      // File I/O errors are reported as exceptions 
      Debug.WriteLine("Exception when taking a photo: " + ex.ToString()); 
     } 

    } 

答えて

2

の後ろにあなたのコード内のフィールドを作成します。

を ボタンのハンドラ内で適切な時間を設定
private int _seconds; 

private void Timer_3sec_Click(object sender, RoutedEventArgs e) 
{ 
    _seconds = 3; 
} 

はフォトボタンハンドラに遅延を追加します。

private async void PhotoButton_Click(object sender, RoutedEventArgs e) 
{ 
    await Task.Delay(TimeSpan.FromSeconds(_seconds)); 
    await TakePhotoAsync(); 
} 

をこれが十分でなければなりません。すべてのロジックを追加しているので、コードが複雑になっていることに気がつくと、MVVMについて読むことを試みます。

+0

ありがとうございました! – thalassophile

+0

カメラの画面に数字(タイマー)を表示することができるかどうかご存じですか?ちょうどそのユーザーがカウントダウンを知っているように – thalassophile

+0

一番上にTextBlockを追加し、テキストのプロパティを変更するだけです。 – cherepets

関連する問題