Array

Using a Chooser to Capture Image Data

Must Read

Admin
Admin
Just post!

The following code example shows how the CameraCaptureCommand delegate command is defined in the constructor for PictureQuestion ViewModel class. This command uses the Capturing property to check whether the application is already in the process of capturing a picture and to control whether the command can be invoked from the UI. The method displays a picture if there is already one saved for this question.

The method then creates a CameraCaptureTask, which will launch the chooser for taking the photograph and return the captured picture.

C#
private readonly CameraCaptureTask task;
public DelegateCommand CameraCaptureCommand { get; set; }
public PictureQuestionViewModel(QuestionAnswer questionAnswer)
: base(questionAnswer)
{
this.CameraCaptureCommand =
new DelegateCommand(this.CapturePicture,
() => !this.Capturing);
if (questionAnswer.Value != null)
{
this.CreatePictureBitmap(questionAnswer.Value);
}
this.task = new CameraCaptureTask();

}

private void CapturePicture()
{
if (!this.Capturing)
{
this.task.Show();
this.Capturing = true;
this.CameraCaptureCommand.RaiseCanExecuteChanged();
}
}

public bool Capturing
{
get { return this.capturing; }
set

{
if (this.capturing != value)
{
this.capturing = value;
this.RaisePropertyChanged(() => this.Capturing);
}
}
}

The following code examples show how the constructor uses the Observable.FromEvent method to specify how to handle the Completed event raised by the CameraCaptureTask chooser object when the user has finished with the chooser. The first example shows how the application saves the picture using a thread from the thread pool, and uses the dispatcher thread to display the picture and re-enable the CameraCaptureCommand command if the user has taken a photograph.

C#
Observable.FromEvent<PhotoResult>(
h => this.task.Completed += h,
h => this.task.Completed -= h)
.Where(e => e.EventArgs.ChosenPhoto != null)
.ObserveOn(Scheduler.ThreadPool)
.Select(a => CreatePictureFile(a.EventArgs.ChosenPhoto))
.ObserveOn(Scheduler.Dispatcher)
.Subscribe(p =>
{
this.Answer.Value = p;
this.CreatePictureBitmap(p);
this.Capturing = false;
this.RaisePropertyChanged(string.Empty);
this.CameraCaptureCommand.RaiseCanExecuteChanged();
});

The second example shows how the CameraCaptureCommand command is re-enabled if the user didn’t take a picture.

C#
Observable.FromEvent<PhotoResult>(
h => this.task.Completed += h,
h => this.task.Completed -= h)
.Where(e => e.EventArgs.ChosenPhoto == null)
.ObserveOn(Scheduler.Dispatcher)
.Subscribe(p =>

{
this.Capturing = false;
this.CameraCaptureCommand.RaiseCanExecuteChanged();
});

 

- Advertisement -

Latest News

Elevate Your Bentley Experience: The Bespoke Elegance of Bentayga EWB by Mulliner

Bentley Motors redefines the essence of bespoke luxury with the introduction of the Bentayga EWB's groundbreaking two-tone customization option—a...
- Advertisement -

More Articles Like This

- Advertisement -