Skip to content

Instantly share code, notes, and snippets.

@KatsuYuzu
Created March 10, 2014 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatsuYuzu/9465516 to your computer and use it in GitHub Desktop.
Save KatsuYuzu/9465516 to your computer and use it in GitHub Desktop.
Windows ストアアプリでWebカメラで写真をキャプチャするトリガーアクション。
using Microsoft.Xaml.Interactivity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.UI.Xaml;
namespace KatsuYuzu.Hoge.Views.Behavior
{
/// <summary>
/// 写真をキャプチャするアクションです。
/// </summary>
class CaputurePhotoAction : DependencyObject, IAction
{
/// <summary>
/// アクションの結果を表します。
/// </summary>
private enum Result
{
Canceled = 0,
Executed = 1
}
/// <summary>
/// アクションの実行後に呼び出す <see cref="ICommand"/> を取得または設定します。
/// </summary>
public ICommand CallbackCommand
{
get { return (ICommand)GetValue(CallbackCommandProperty); }
set { SetValue(CallbackCommandProperty, value); }
}
public static readonly DependencyProperty CallbackCommandProperty =
DependencyProperty.Register("CallbackCommand",
typeof(ICommand),
typeof(CaputurePhotoAction),
new PropertyMetadata(null));
/// <summary>
/// キャプチャされた写真を格納する形式を取得または設定します。
/// </summary>
public CameraCaptureUIPhotoFormat PhotoFormat
{
get { return (CameraCaptureUIPhotoFormat)GetValue(PhotoFormatProperty); }
set { SetValue(PhotoFormatProperty, value); }
}
public static readonly DependencyProperty PhotoFormatProperty =
DependencyProperty.Register("PhotoFormat",
typeof(CameraCaptureUIPhotoFormat),
typeof(CaputurePhotoAction),
new PropertyMetadata(CameraCaptureUIPhotoFormat.Jpeg));
/// <summary>
/// アクションを実行します。
/// </summary>
/// <param name="sender">使用されません。</param>
/// <param name="parameter">使用されません。</param>
/// <returns>アクションの結果を返します。</returns>
public object Execute(object sender, object parameter)
{
if (this.CallbackCommand == null)
{
return Result.Canceled;
}
// 警告の抑制
var task = ExecuteAsync(this.PhotoFormat, this.CallbackCommand);
return Result.Executed;
}
/// <summary>
/// アクションを実行します。
/// </summary>
/// <param name="format">キャプチャされた写真を格納する形式を決定します。</param>
/// <param name="callbakCommand">アクションの実行後に呼び出す <see cref="ICommand"/>。</param>
/// <returns></returns>
private async static Task ExecuteAsync(CameraCaptureUIPhotoFormat format, ICommand callbakCommand)
{
var photo = await CaputurePhotoAsync(format);
if (!callbakCommand.CanExecute(photo))
{
return;
}
callbakCommand.Execute(photo);
}
/// <summary>
/// 写真をキャプチャします。
/// </summary>
/// <param name="format">キャプチャされた写真を格納する形式を決定します。</param>
/// <returns></returns>
private async static Task<StorageFile> CaputurePhotoAsync(CameraCaptureUIPhotoFormat format)
{
var camera = new CameraCaptureUI();
camera.PhotoSettings.Format = format;
return await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment