CameraCaptureTask的用法
首先要引用相关的名字空间:using Microsoft.Phone.Tasks;
然后在Page里定义一个变量:CameraCaptureTask cameraCaptureTask;
然后要在Page的Constructor里添加实例化和关联Completed事件处理方法:
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
如下代码所示:
代码
public partial class MainPage : PhoneApplicationPage
{
CameraCaptureTask cameraCaptureTask;
// Constructor
public MainPage()
{
InitializeComponent();
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
}
}
{
CameraCaptureTask cameraCaptureTask;
// Constructor
public MainPage()
{
InitializeComponent();
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
}
}
然后写Completed事件处理方法:
代码
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
//save the captured photo to settings
(Application.Current as App).photoTaked = new BitmapImage();
(Application.Current as App).photoTaked = bmp;
//MessageBox.Show(e.OriginalFileName);
}
else
{
(Application.Current as App).photoTaked = null;
}
}
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
//save the captured photo to settings
(Application.Current as App).photoTaked = new BitmapImage();
(Application.Current as App).photoTaked = bmp;
//MessageBox.Show(e.OriginalFileName);
}
else
{
(Application.Current as App).photoTaked = null;
}
}
然后是在调用摄像头的地方加入show()方法:
void retake_Click(object sender, EventArgs e)
{
cameraCaptureTask.Show();
}