I have a simple application that has a single page with a button that invokes the CameraCaptureTask in Windows Phone 7. I use the MVVM pattern for this. My code behind is clean and I have offloaded the button click response to the ViewModel using behaviors. My code looks like this:
public class MainViewModel : ViewModelBase
{
private readonly CameraCaptureTask cameraCaptureTask;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += cameraCaptureTask_Completed;
CaptureCommand = new RelayCommand(() => CaptureImage());
}
}
public RelayCommand CaptureCommand { get; set; }
private void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e == null || e.TaskResult != TaskResult.OK)
{
return;
}
else
{
// TODO
}
}
private object CaptureImage()
{
cameraCaptureTask.Show();
return null;
}
}
Now when I run my application and hit the button that binds to the ‘CaptureCommand’ RelayCommand, I hit my breakpoint in the ‘CaptureCommand()’ method and it fires the ‘Show()’ method of the CameraCaptureTask resulting in my camera coming up on the device successfully. However once I complete taking a picture and hit ‘Accept’, the ‘cameraCaptureTask_Completed(object sender, Photoresult e)’ event handler never gets invoked. Am I missing something here?
Thanks in advance!
Why are you relaying this when you have just a show?
Try moving the cameraCaptureTask out of the Constructor and into an invoked method